我在C#和WPF中做简单的跳棋游戏。我面临的问题与正确显示内容有关。我有一个棋盘,我可以把棋子放在我想要的任何地方,但不幸的是我无法进行任何数字移动(我的愿景是玩家点击Pawn / Queen - 这个数字现在可以用光标移动 - 然后他放了它在正确的位置)。我尝试从父母删除图像并添加到不同的网格和其他一些想法 - 但我失败了。
<Window x:Class="Checkers.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Checkers"
Title="Checkers" WindowStyle="ThreeDBorderWindow" MinWidth="800" MinHeight="680" Width="800" Height="680" Icon="C:\Users\NAME\Documents\Visual Studio 2012\Projects\Checkers\Checkers\Resources\package_games_board.ico" WindowStartupLocation="CenterScreen" >
<Window.Resources>
<DataTemplate DataType="{x:Type local:Pawn}">
<Image Source="{Binding ImageSource}" MouseDown="ItemsControl_MouseDown_1" MouseMove="helo_MouseMove_1" Name="helo" Tag="{Binding Reference}"/>
</DataTemplate>
</Window.Resources>
<Grid x:Name="LayoutRoot" Background="MidnightBlue">
<Grid.RowDefinitions>
<RowDefinition Height="20"/>
<RowDefinition Height="600"/>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Menu Grid.Row="0" Grid.Column="1" Height="18" VerticalAlignment="Top" Margin="0,0,0,0"/>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="600" />
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<UniformGrid Grid.Column="1" x:Name="CheckersBoard" Rows="8" Columns="8" SnapsToDevicePixels="False"/>
<ItemsControl Grid.Column="1" ItemsSource="{Binding}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate x:Name="GridBoard">
<Grid IsItemsHost="True">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
</Grid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Grid.Row" Value="{Binding Row}"/>
<Setter Property="Grid.Column" Value="{Binding Column}"/>
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
</Grid>
</Grid>
然后我有Pawn班:
public class Pawn : INotifyPropertyChanged
{
public bool IsBlack { get; set; }
public Pawn Reference { get; set; }
private int _row;
public Pawn()
{
Reference = this;
}
public int Row
{
get { return _row; }
set
{
_row = value;
OnPropertyChanged("Row");
}
}
private int _column;
public int Column
{
get { return _column; }
set
{
_column = value;
OnPropertyChanged("Column");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
public string ImageSource
{
get { return "C:\\Users\\NAME\\Documents\\Visual Studio 2012\\Projects\\Checkers\\Checkers\\Resources\\" + (IsBlack ? "black" : "white") + "_transparent.png"; }
}
}
主要代码(其余情况并非如此):
public partial class MainWindow : Window
{
private ObservableCollection<Pawn> Pawns;
private void ShowBoard()
{
SolidColorBrush scb_white = (SolidColorBrush)(new BrushConverter().ConvertFrom("#8080FF"));
SolidColorBrush scb_dark = (SolidColorBrush)(new BrushConverter().ConvertFrom("#004A95"));
for (int i = 0; i < 64; i++)
CheckersBoard.Children.Add(new Rectangle() { Name = "field"+i, Stroke=Brushes.Black, Fill = (i + 1 + i / 8) % 2 == 0 ? scb_white : scb_dark });
Pawns.Add(new Pawn() { Row = 0, Column = 0, IsBlack = true });
Pawns.Add(new Pawn() { Row = 0, Column = 2, IsBlack = false });
Pawns.Add(new Pawn() { Row = 0, Column = 4, IsBlack = true });
}
public MainWindow()
{
Pawns = new ObservableCollection<Pawn>();
InitializeComponent();
DataContext = Pawns;
ShowBoard();
}
Mouse handlers ...
有人知道解决方案吗?
答案 0 :(得分:0)
对于您拥有的代码,只需在Pawn对象上设置行/列就像它将执行&#34;运动&#34;你要。如果它不起作用,请检查输出窗口是否有绑定异常。
话虽如此,这只会让他们全身心投入。对于真正的移动,您需要编写Storyboard(可能在代码隐藏中)并移动其位置。最好的方法是通过TranslateTransform。
<强>更新强>
在这种系统中进行拖放操作将非常困难并且效率不高。您可能需要查看此问题/答案:Dragging a WPF user control