2017-01-07 4 views
2

カスタムフィールドタイプKFieldにバインドされたボタンを配置したいと思い、Xの形状に5つのボタンを配置する必要があります.3x3グリッドのボタンが必要です4辺、中央にあります。私は、次のフィールドタイプがあります。WPF - アイテムテンプレートを使用したグリッドの使用

public class KField : ViewModelBase // ViewModelBase is a custom abstract INotifyPropertyChanged 
{ 
    private char _text; 
    private bool _isEnabled; 
    private int _x; 
    private int _y;  

    public int X { get { return _x; } set { _x = value; OnPropertyChanged(); } } 
    public int Y { get { return _y; } set { _y = value; OnPropertyChanged(); } } 
    public bool IsEnabled 
    { 
     get { return _isEnabled; } 
     set 
     { 
      if (_isEnabled != value) 
      { 
       _isEnabled = value; 
       OnPropertyChanged(); 
      } 
     } 
    } 

    public char Text 
    { 
     get { return _text; } 
     set 
     { 
      if (_text != value) 
      { 
       _text = value; 
       OnPropertyChanged(); 
      } 
     } 
    } 

    public DelegateCommand ClickCommand { get; set; } 
} 

をそして私はすべてのボタンが含まれており、ビューにバインドされます_fields変数、使用:

_fields = new ObservableCollection<KField>(); 
_fields.Add(new KField { Text = _model.ModelFields[0], IsEnabled = true, X = 0, Y = 0}); 
_fields.Add(new KField { Text = _model.ModelFields[1], IsEnabled = false, X = 0, Y = 2 }); 
_fields.Add(new KField { Text = _model.ModelFields[2], IsEnabled = false, X = 1, Y = 1 }); 
_fields.Add(new KField { Text = _model.ModelFields[3], IsEnabled = true, X = 2, Y = 0 }); 
_fields.Add(new KField { Text = _model.ModelFields[4], IsEnabled = false, X = 2, Y = 2 }); 

を私はもちろんのFieldsプロパティを作成:

public ObservableCollection<KField> Fields 
    { 
     get { return _fields; } 
     set 
     { 
      if (_fields != value) 
      { 
       _fields = value; 
       OnPropertyChanged(); 
      } 
     } 
    } 

そして私は、このXAMLを使用しています:

<Window x:Class="WpfApplication2.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:local="clr-namespace:WpfApplication2" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="376" Width="534"> 
    <Grid x:Name="myGrid"> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition/> 
      <ColumnDefinition/> 
      <ColumnDefinition/> 
     </Grid.ColumnDefinitions> 
     <Grid.RowDefinitions> 
      <RowDefinition/> 
      <RowDefinition/> 
      <RowDefinition/> 
     </Grid.RowDefinitions> 
     <ItemsControl ItemsSource="{Binding Fields}" Margin="0,0,0,35"> 
      <ItemsControl.ItemTemplate> 
       <DataTemplate> 
        <Button Grid.Column="{Binding Y}" Grid.Row="{Binding X}" BorderThickness="0.3 " BorderBrush="Black" Command="{Binding ButtonPressed}" Content="{Binding Text}" IsEnabled="{Binding IsEnabled}"> 
        </Button> 
       </DataTemplate> 
      </ItemsControl.ItemTemplate> 
     </ItemsControl> 
    </Grid> 
</Window> 

しかし、これは私のすべてのボタンをグリッドの最初のセルに置きます。テキストと有効/無効の状態は正常に機能するので、バインドは行われますが、すべてのXプロパティとYプロパティは無視されます。私は何が欠けていますか?これらのボタンは、どうやって私が望むように配置できますか?ありがとうございました!

+1

私が正しい場合、私は知らないが、私はあなたが内部のグリッドを配置する必要があると思います'ItemsControl'と、おそらくあなたの' DataTemplate'の中にあるかもしれません。それを試して、何か変わったかどうか確認してくださいボタンを先祖のグリッドに配置すると言っているからですが、先祖はグリッドではなく、それが理にかなっている場合はデータ型です。それが役立つかどうかを見てください。 – Markinson

+0

これが役立つ場合は、次のリンクを参照してください。https://wpf.2000things.com/2011/12/21/455-using-itemcontainerstyle-to-bind-data-elements-in-a-collection-to-a-grid/ – Markinson

答えて

1

問題は、ItemsControlがグリッドからちょうど離れていたことでした。私はItemsControl内のグリッドを入れて、ItemContainerStyleの座標の結合を提供する場合、それが正常に動作します:

<Window x:Class="WpfApplication2.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:local="clr-namespace:WpfApplication2" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="376" Width="534"> 
    <ItemsControl x:Name="MyItems" ItemsSource="{Binding Fields}"> 
     <ItemsControl.ItemsPanel> 
      <ItemsPanelTemplate> 
       <Grid> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition /> 
         <ColumnDefinition /> 
         <ColumnDefinition /> 
        </Grid.ColumnDefinitions> 
        <Grid.RowDefinitions> 
         <RowDefinition /> 
         <RowDefinition /> 
         <RowDefinition /> 
        </Grid.RowDefinitions> 
       </Grid> 
      </ItemsPanelTemplate> 
     </ItemsControl.ItemsPanel> 
     <ItemsControl.ItemTemplate> 
      <DataTemplate> 
       <Button BorderThickness="0.3 " BorderBrush="Black" Command="{Binding ButtonPressed}" Content="{Binding Text}" IsEnabled="{Binding IsEnabled}"> 
       </Button> 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
     <ItemsControl.ItemContainerStyle> 
      <Style> 
       <Style.Setters> 
        <Setter Property="Grid.Row" Value="{Binding X}" /> 
        <Setter Property="Grid.Column" Value="{Binding Y}" /> 
       </Style.Setters> 
      </Style> 
     </ItemsControl.ItemContainerStyle> 
    </ItemsControl> 
</Window> 
+1

あなたはようこそhaha – Markinson

+2

ItemTemplateの代わりにXバインディングとYバインディングを入れなければならないItemContainerStyleの使用について言及することを忘れないでください。 – Clemens

+0

私は実際にあなたがそれを行うことができるか分からなかった!私はまだ多くのことを学んでいると思います:) – Encrypt0r

関連する問題