如何在WPF中分解DataTemplate的绑定?

时间:2022-09-28 23:03:55

I have a DataTemplate I want to reuse. The part I want to factor out is the binding, because it's the only thing that changes. My DataTemplate looks roughly like this. (There's actually quite a bit more to it, but I've taken out the extraneous stuff.)

我有一个我想重用的DataTemplate。我要分解的部分是绑定,因为它是唯一改变的东西。我的DataTemplate看起来大致如此。 (实际上还有更多的东西,但我已经拿出了无关紧要的东西。)

<DataTemplate>
    <TextBox Text="{Binding Name}" />
</DataTemplate>

How can I reuse this DataTemplate while simply varying the property to which I'm binding? (Note that if it were as simple as just a TextBox, I wouldn't worry about it, but the DataTemplate actually contains a StackPanel with a number of other elements in it. I want to centralize that in one place, hence the DataTemplate.)

如何简单地改变我绑定的属性,我如何重用这个DataTemplate? (请注意,如果它只是一个TextBox那么简单,我不会担心它,但DataTemplate实际上包含一个StackPanel,其中包含许多其他元素。我想将它集中在一个地方,因此DataTemplate。 )

I've thought about two ways to tackle this problem.

我想过两种解决这个问题的方法。

  1. Create a simple custom control. Reuse that, and don't worry about reusing the DataTemplate.
  2. 创建一个简单的自定义控件重用它,不要担心重用DataTemplate。
  3. Experiment with some kind of subclass of DataTemplate. (I'm told this is possible.) I'd add a dependency property to it that lets me specify the name of the property to which I want to bind.
  4. 尝试使用某种DataTemplate子类。 (我被告知这是可能的。)我将向它添加一个依赖属性,它允许我指定要绑定的属性的名称。

Suggestions?

建议?

2 个解决方案

#1


4  

I hate answering my own questions, but for the sake of completeness, here's my solution.

我讨厌回答我自己的问题,但为了完整起见,这是我的解决方案。

<ListBox ItemsSource="{Binding}">
  <ListBox.Resources>
    <ControlTemplate x:Key="textBoxControlTemplate" TargetType="ContentControl">
      <TextBox Text="{TemplateBinding Content}" /> 
    </ControlTemplate>
  </ListBox.Resources>
  <ListBox.ItemTemplate>
    <DataTemplate>
      <ContentControl Content="{Binding Name}" Template="{StaticResource textBoxControlTemplate}" />
    </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>

This of course is a very contrived example. In my own app, I'm not actually putting textboxes inside of a listbox. In a listbox, this is not very useful, but imagine it inside of a DataGrid, where each column might be displayed in a similar way, but binds to a different property.

这当然是一个非常人为的例子。在我自己的应用程序中,我实际上并没有将文本框放在列表框中。在列表框中,这不是很有用,但想象一下它在DataGrid中,其中每列可能以类似的方式显示,但绑定到不同的属性。

#2


0  

Create a UserControl and use it within the DataTemplate.

创建UserControl并在DataTemplate中使用它。

<DataTemplate>
    <local:MyComplexUserControl DataContext="{Binding Name}"/>
</DataTemplate>

and within the UserControl:

在UserControl中:

<StackPanel>
  <TextBlock>Value:</Text>
  <TextBox Text="{Binding}"/>
</StackPanel>

Have a separate DataTemplate with its own binding for each occasion.

每个场合都有一个单独的DataTemplate,它有自己的绑定。

#1


4  

I hate answering my own questions, but for the sake of completeness, here's my solution.

我讨厌回答我自己的问题,但为了完整起见,这是我的解决方案。

<ListBox ItemsSource="{Binding}">
  <ListBox.Resources>
    <ControlTemplate x:Key="textBoxControlTemplate" TargetType="ContentControl">
      <TextBox Text="{TemplateBinding Content}" /> 
    </ControlTemplate>
  </ListBox.Resources>
  <ListBox.ItemTemplate>
    <DataTemplate>
      <ContentControl Content="{Binding Name}" Template="{StaticResource textBoxControlTemplate}" />
    </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>

This of course is a very contrived example. In my own app, I'm not actually putting textboxes inside of a listbox. In a listbox, this is not very useful, but imagine it inside of a DataGrid, where each column might be displayed in a similar way, but binds to a different property.

这当然是一个非常人为的例子。在我自己的应用程序中,我实际上并没有将文本框放在列表框中。在列表框中,这不是很有用,但想象一下它在DataGrid中,其中每列可能以类似的方式显示,但绑定到不同的属性。

#2


0  

Create a UserControl and use it within the DataTemplate.

创建UserControl并在DataTemplate中使用它。

<DataTemplate>
    <local:MyComplexUserControl DataContext="{Binding Name}"/>
</DataTemplate>

and within the UserControl:

在UserControl中:

<StackPanel>
  <TextBlock>Value:</Text>
  <TextBox Text="{Binding}"/>
</StackPanel>

Have a separate DataTemplate with its own binding for each occasion.

每个场合都有一个单独的DataTemplate,它有自己的绑定。