2011-01-06 17 views
0

への結合:は、私は次のXAMLを持ってItemsPresenterプロパティ

<ItemsControl ItemsSource="{Binding...}" > 
    <ItemsControl.Template> 
     <ControlTemplate> 
      <ItemsPresenter x:Name="testGrid"/> 
     </ControlTemplate> 
    </ItemsControl.Template> 
    <!--Use the ItemsPanel property to specify a custom UniformGrid that 
    holds the laid out items.--> 
    <ItemsControl.ItemsPanel> 
     <ItemsPanelTemplate> 
      <tools:UniformGridRtL Columns="8" x:Name="testGrid2" /> 
     </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel> 

    <!--Use the ItemTemplate to set a DataTemplate to define 
     the visualization of the data objects. This DataTemplate 
     specifies that each data object appears RegisterBit appears 
     as a CheckBox bound to RegisterBit properties. It also defines 
     a custom template for the checkbox.--> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <CheckBox... /> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 

<Label> 
    <Binding ElementName="testGrid2" Path="property_of_UniformGridRtL"/> 
</Label> 

基本的に、私はItemsControlにでItemsPresenterをテンプレートますItemsPanelTemplateとして設定されたカスタムパネル(UniformGridRtL)を、持っています。 UniformGridRtLにはバインドしたいプロパティがありますが、ElementNameはLabel Bindingで機能していないようです。 生成されたItemsControlアイテムホストのプロパティにどのようにバインドできますか?

答えて

0

ElementNameバインディングソースは、テンプレート化されたアイテム、通常はテンプレート化アイテムが1つしかないItemsPanelTemplateアイテムでも機能しません。問題は、それがテンプレートであるため、理論上複数の要素を持つことができるため、WPFはバインドする名前付き項目を認識しません。

private void grid_Loaded(object sender, RoutedEventArgs e) 
{ 
    Binding binding = new Binding("NameOfGridPropertyToBindTo"); 
    binding.Source = sender; 
    boundLabel.SetBinding(Label.ContentProperty, binding); 
} 

上記のコードは、<Label Name="boundLabel"/>ためのようなものを想定している:次に回避策として

、(この場合<tools:UniformGridRtL Loaded="grid_Loaded" .../>に)パネルのLoadedイベントをサブスクライブしてみてください、そして、コードで手動バインディングを設定しますあなたのラベル宣言。

+0

回避策ありがとう...バインディングターゲットがテンプレートアイテムでない場合は、実際に動作します。同じItemsControl ControlTemplate内の兄弟要素にバインドする場合はどうなりますか? – Mart

+0

送信元の使用(namedItemsControl).Template.FindName()をここで説明するように使用する代わりに、兄弟ソース要素がある場合:http://stackoverflow.com/questions/820201/how-to-access-a-wpf- control-located-in-a-controltemplate –

+0

申し訳ありませんが、私のコメントは、ターゲットがテンプレート化されている場合、Template.FindName()と同じ戦略を使用することができます - コードはちょっと混乱します。まだ何らかの種類のLoadedイベントを使用するようにしてください。そうしないと、ビジュアルツリーはまだ作成されず、FindNameはnullを返します。 –

関連する問題