我是WPF的新手,我使用此代码在网格中填充了统一网格,
public MainWindow()
{
InitializeComponent();
SolidColorBrush defaultBrush = new SolidColorBrush(Colors.Wheat);
SolidColorBrush alternateBrush = new SolidColorBrush(Colors.Black);
Char L = 'A';
int N = 1;
for (int i = 0; i < 64; i++)
{
Grid cell = new Grid();
if(N==9)
{
N=1;
L++;
}
if ((i + i / 8) % 2 == 0)
{
cell.Name = L + N.ToString();
cell.Background = defaultBrush;
ChessBoard.Children.Add(cell);
}
else
{
cell.Name = L + N.ToString();
cell.Background = alternateBrush;
ChessBoard.Children.Add(cell);
}
N++
}
即时通讯然后当我单击名为ChessBoard的统一网格时,试图找出某个网格的名称。
private void ChessBoard_MouseLeftButtonDown(object sender, MouseButtonEventArgs args)
{
var element = (UIElement)args.Source;
element.Opacity = 0.5;
}
测试不透明度线以确保im在正确的网格上,并且它可以工作并更改单击im时网格的不透明度。
我需要帮助的是找到元素的Name属性。
好的,删除所有代码并重新开始。
如果您使用的是WPF,则确实需要了解并接受WPF的心态。
通常,不建议在过程代码中创建或操作UI元素。相反,您将创建一个适当的ViewModel,该ViewModel将保存代表UI所显示的状态和数据的所有相关属性,并使用DataBinding反映这些属性。
这是您在WPF中所做的尝试:
<Window x:Class="ChessBoardSample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="350">
<ItemsControl ItemsSource="{Binding Squares}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Rows="8" Columns="8"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button x:Name="Square"
Command="{Binding DataContext.SquareClickCommand, RelativeSource={RelativeSource AncestorType=ItemsControl}}"
CommandParameter="{Binding}">
<Button.Template>
<ControlTemplate TargetType="Button">
<Grid Background="{TemplateBinding Background}"/>
</ControlTemplate>
</Button.Template>
</Button>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IsBlack}" Value="True">
<Setter TargetName="Square" Property="Background" Value="Black"/>
</DataTrigger>
<DataTrigger Binding="{Binding IsBlack}" Value="False">
<Setter TargetName="Square" Property="Background" Value="Wheat"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Window>
背后的代码:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new ChessBoard();
}
}
ViewModel:
public class ChessBoard
{
public List<ChessSquare> Squares { get; private set; }
public Command<ChessSquare> SquareClickCommand { get; private set; }
public ChessBoard()
{
Squares = new List<ChessSquare>();
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
Squares.Add(new ChessSquare() {Row = i, Column = j});
}
}
SquareClickCommand = new Command<ChessSquare>(OnSquareClick);
}
private void OnSquareClick(ChessSquare square)
{
MessageBox.Show("You clicked on Row: " + square.Row + " - Column: " + square.Column);
}
}
数据项:
public class ChessSquare
{
public int Row { get; set; }
public int Column { get; set; }
public bool IsBlack { get { return (Row + Column) %2 == 1; }}
}
命令类(MVVM帮助程序类):
public class Command<T>: ICommand
{
public Action<T> Action { get; set; }
public void Execute(object parameter)
{
if (Action != null && parameter is T)
Action((T)parameter);
}
public bool CanExecute(object parameter)
{
return IsEnabled;
}
private bool _isEnabled = true;
public bool IsEnabled
{
get { return _isEnabled; }
set
{
_isEnabled = value;
if (CanExecuteChanged != null)
CanExecuteChanged(this, EventArgs.Empty);
}
}
public event EventHandler CanExecuteChanged;
public Command(Action<T> action)
{
Action = action;
}
}
结果:
尽管所有这些看起来都像是“太多的代码”,但是如果仔细观察,您会发现其中的大多数实际上是可重用的,干净的代码,并且与UI无关,并且我还将为您提供许多可重用的基础结构( (例如Command
上课)。
我使用的ItemsControl
是数据项集合的绑定,而不是按程序创建UI元素。这就是“让WPF发挥作用”。
DataContext = ...
。WPF中的后面代码保留给UI特定代码,而不是与数据或逻辑相关的代码。DelegateCommand
来处理Click逻辑,而不是事件处理程序。这在由ItemsControl等动态创建UI元素的情况下很有用。OnSquareClick()
方法是如何针对您自己的简单的有状态数据项(ChessSquare
)而不是复杂的WPF UI对象进行操作的。这就是您在WPF中编程的方式。您对数据而不是UI进行操作。IsBlack
来动态更改背景颜色。File -> New Project -> WPF Application
然后自己查看结果。本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句