2016-11-21 46 views
0

我有一個窗口,我基本上建立貧民區掃雷。英寸我有一個網格,我喂入鋸齒陣列,設置,以便它將適應任何變化在數組的大小(沒有硬設置值或行/列)。最重要的是,我有一個空白按鈕的網格,只是簡單地適應下面的網格。WPF C#從點擊事件獲取網格按鈕的陣列位置

當你點擊一個按鈕時,它會隱藏顯示它下面的值,我需要一些方法來返回被點擊的按鈕的位置,所以我可以比較原始的參差不齊的數組,以找出項目是否是一個炸彈。 (這也可以幫助我在空白的瓷磚上進行填充動作)。但考慮到如何設置,Grid.GetRow或Grid.GetColumn只返回0。

有誰知道我可以如何從設置中獲取數組的位置(最好是行和列)?

XAML下面是C#單擊事件。

<Window x:Class="MinesweeperWPF.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="Minesweeper" SizeToContent="WidthAndHeight" Height="Auto" Width="Auto"> 
    <Window.Resources> 
     <DataTemplate x:Key="Buttons_Template"> 
      <Button Content="" 
        Height="20" 
        Width="20" 
        Margin="0,0,0,0" 
        Visibility="Visible" 
        Click="ButtonClick" 
        MouseRightButtonUp="RightClick"/> 
     </DataTemplate> 
     <DataTemplate x:Key="DataTemplate_2"> 
      <TextBlock Text="{Binding}" 
          Height="20" 
          Width="20" 
          Margin="0,0,0,0" 
          FontFamily="Rockwell" 
          HorizontalAlignment="Center" 
          VerticalAlignment="Center" 
          Padding="5"/> 
     </DataTemplate> 
     <DataTemplate x:Key="DataTemplate_1"> 
      <ItemsControl ItemsSource="{Binding}" ItemTemplate="{DynamicResource DataTemplate_2}"> 
       <ItemsControl.ItemsPanel> 
        <ItemsPanelTemplate> 
         <StackPanel Orientation="Horizontal"/> 
        </ItemsPanelTemplate> 
       </ItemsControl.ItemsPanel> 
      </ItemsControl> 
     </DataTemplate> 
     <DataTemplate x:Key="Buttons_Template2"> 
      <ItemsControl ItemsSource="{Binding}" ItemTemplate="{DynamicResource Buttons_Template}"> 
       <ItemsControl.ItemsPanel> 
        <ItemsPanelTemplate> 
         <StackPanel Orientation="Horizontal"/> 
        </ItemsPanelTemplate> 
       </ItemsControl.ItemsPanel> 
      </ItemsControl> 
     </DataTemplate> 
    </Window.Resources> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="*"/> 
     </Grid.RowDefinitions> 
     <Grid> 
      <TextBlock x:Name="RemainingMines" HorizontalAlignment="Left" /> 
      <TextBlock x:Name="Difficulty" HorizontalAlignment="Center" /> 
      <TextBlock x:Name="Timer" HorizontalAlignment="Right" /> 
     </Grid> 
     <Grid Name="ResetButton" Grid.Row="1"> 
      <Button Name="Reset" Content="Reset"/> 
     </Grid> 
     <Grid Name="GridBoard" ShowGridLines="True" Grid.Row="2"> 
      <ItemsControl x:Name="GridItems" ItemTemplate="{DynamicResource DataTemplate_1}"/> 
     </Grid> 
     <Grid Name="ButtonsBoard" ShowGridLines="True" Grid.Row="2"> 
      <ItemsControl x:Name="ButtonItems" ItemTemplate="{DynamicResource Buttons_Template2}"/> 
     </Grid> 
    </Grid> 
</Window> 

在C#

private void ButtonClick(object sender, RoutedEventArgs e) 
{ 
    int col = Grid.GetColumn((Button)sender); //this just returns 0 
    int row = Grid.GetRow((Button)sender); //this just returns 0 
    Button source = e.Source as Button; 
    source.Visibility = Visibility.Hidden;   
    Console.WriteLine("L: {0} x {1}", col, row); //prints L: 0 x 0 
} 
private void RightClick(object sender, RoutedEventArgs e) 
{ 
    int col = Grid.GetColumn((Button)sender); //this just returns 0 
    int row = Grid.GetRow((Button)sender); //this just returns 0 
    Button source = e.Source as Button; 
    if(source.Content.Equals("")) 
    { 
     source.Content = "\u2691"; 
    } 
    else 
    { 
     source.Content = ""; 
    } 
    Console.WriteLine("R: {0} x {1}", col, row); //prints R: 0 x 0 
} 

任何幫助,請點擊方法將在此不勝感激。

+0

您是否嘗試過'e.Source作爲UIElement'代替'Button'? – Searching

+0

您是否熟悉MVVM?並且,你能提供一幅你想要達成的目標嗎? –

+0

var index = Array.FindIndex(Array,x => x == buttonClicked),這可以工作嗎?然後返回索引? – JohnChris

回答

0

您需要使用適當的按鈕控制託管。在你的xaml中,你正在Grid中使用項目控件。但是Items控件沒有行和列索引。這就是爲什麼你無法獲得索引。 使用UniformGrid或一些相關的控件。

你可以參考這篇文章

https://stackoverflow.com/a/13588066/5273015

會幫助你很多其他任務以及