2014-04-21 54 views
2

我正在C#和WPF中進行簡單的跳棋遊戲。我面臨的問題與正確顯示內容有關。我有一個棋盤,我可以將棋子放在任何我想要的位置,但不幸的是我無法做出任何動作(我的願景是玩家點擊Pawn/Queen - 這個圖形現在可以用光標移動 - 然後他把它放在上面正確的位置)。我試圖從父母移除圖像,並添加到不同的網格和一些其他的想法 - 但我失敗了。跳棋和活動數字WPF

<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> 

然後,我有類典當的:

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

關於真正的運動概念: 我不知道如何將鼠標位置分配給網格列和行以及如何避免我只能在特定的網格行和列(字段)。 或者我可能沒有想到。 – user3558203

回答

0

對於您的代碼,只需在Pawn對象上設置Row/Column就會出現,就像它會執行所需的「移動」一樣。如果綁定異常不起作用,請檢查輸出窗口是否有綁定異常。

這就是說,這隻會讓他們都跳來跳去。對於真實的移動您將需要編寫Storyboard(可能在代碼隱藏中)並移動他們的位置。最好的方法是通過TranslateTransform。

更新

做拖放的方式,這種系統將是非常困難的,不是非常有效的。你可能想看看這個問題/答案:Dragging a WPF user control