2011-07-21 46 views
1

ItemsPanelが15 * 15の均一なグリッドであるItemsControlを作成するXAMLのカスタムユーザーコントロールがあります。 ItemsControlには起動時にCellが設定されます。カスタムユーザーコントロールのItemsControlの子を2次元配列にバインドする方法WPF C#?

<UserControl x:Class="Words.GameBoard" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:Words="clr-namespace:Words" 
     xmlns:Cell="Words.CellCollection" 
     xmlns:CellTile="Words.Cell" 
     mc:Ignorable="d" 
     d:DesignHeight="300" d:DesignWidth="300"> 
<UserControl.Resources> 
    <Words:CellCollection x:Key="CellCollectionData"> 
    </Words:CellCollection> 
    <Style TargetType="ItemsControl"> 
     <Setter Property="ItemsPanel"> 
      <Setter.Value> 
       <ItemsPanelTemplate> 
        <UniformGrid IsItemsHost="True" Margin="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/> 
       </ItemsPanelTemplate> 
      </Setter.Value> 
     </Setter> 
     <Setter Property="ItemTemplate"> 
      <Setter.Value> 
       <DataTemplate> 
        <Words:Cell> 
        </Words:Cell> 
       </DataTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</UserControl.Resources> 
<ItemsControl Name="BoardControl"> 
</ItemsControl> 

コントロールは、私は配列へのItemsControlの項目をバインドするにはどうすればよいこの

  <clr:GameBoard> 
      </clr:GameBoard> 

のようにメインウィンドウに追加されますか? Observablesやものは必要ありません。ボタンをクリックするとアイテムが更新されます。これは可能ですか?私は両方のC#とXAMLに非常に新しいまし

おかげ

答えて

3

あなたはこのような配列をバインドするためにあなたのItemsControl上のItemsSourceを設定する必要があります。

<ItemsControl Name="BoardControl" ItemsSource={DynamicResource CellCollectionData} /> 

私はあなたがバインドされCellCollectionDataを望んでいたと仮定しています。しかし、典型的には、私はあなたが使用している場合にはDataContextの中にすべきデータを、期待:

<ItemsControl Name="BoardControl" ItemsSource={Binding} /> 

(UserControlのDataContextのが配列に設定されている場合)

または

<ItemsControl Name="BoardControl" ItemsSource={Binding MyArray} /> 

(UserControlのDataContextが配列を返したプロパティを含むクラスの場合)

学習中およびテスト中に、私はIte msControlをListBoxに追加すると、アイテムが実際に追加されていることがわかります(選択可能なアイテムをクリックして)。一見を得たら、それをItemsControl(アイテムの選択を許可しない)に戻すことができます。

0

あなたは配列にバインドすることができ、プロパティのItemsSource =「{バインディングパス= ArrayProperty}」 私はあなたが接続されている配列を持つモデルがあると設定このビューに。