wpf 如何在资源字典中绑定数据模板
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29776976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I bind datatemplate in a resource dictionary
提问by cdie
I'm trying to bind my elements in a datatemplate that is define in dictionary. Let's make it simple.
我正在尝试将我的元素绑定在字典中定义的数据模板中。让我们简单点。
I have a simple class
我有一个简单的类
public class A { public string Data {get;set} }
I have a simple view that contains a ListBox, with ItemSources is a list of class A :
我有一个包含 ListBox 的简单视图,其中 ItemSources 是 A 类的列表:
<ListBox ItemsSource="{Binding AList}">
The point is, when I define Itemplate in view directly, bind works :
关键是,当我直接在视图中定义 Itemplate 时,bind 工作:
<ListBox.ItemTemplate>
<DataTemplate >
<TextBlock Text="{Binding Data}" />
<Rectangle Fill="Red" Height="10" Width="10"/>
</DataTemplate>
</ListBox.ItemTemplate>
This works great.
这很好用。
But when I define this ItemTemplate in resource Dictionary, binding doesn't works ?
但是当我在资源字典中定义这个 ItemTemplate 时,绑定不起作用?
How can I do that ?
我怎样才能做到这一点 ?
PS : This is a simple example to explain my problem, don't tell me to override toString function to make it works or use classe template, my real case is very more complexe than this.
PS:这是一个简单的例子来解释我的问题,不要告诉我覆盖 toString 函数使其工作或使用 classe 模板,我的真实情况比这更复杂。
Thanks for help
感谢帮助
回答by lerner1225
Create a new Dictionary1.xaml
创建一个新的 Dictionary1.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<DataTemplate x:Key="dataTemplate">
<StackPanel>
<TextBlock Text="{Binding Data}" />
<Rectangle Fill="Red" Height="10" Width="10"/>
</StackPanel>
</DataTemplate>
</ResourceDictionary>
In MainWindow.xaml refer it
在 MainWindow.xaml 中引用它
<Window.Resources>
<ResourceDictionary Source="Dictionary1.xaml" />
</Window.Resources>
<ListBox Name="lst" ItemTemplate="{StaticResource dataTemplate}"></ListBox>
MainWindow.cs:
主窗口.cs:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
{
var observable = new ObservableCollection<Test>();
observable.Add(new Test("A"));
observable.Add(new Test("B"));
observable.Add(new Test("C"));
this.lst.ItemsSource = observable;
}
}
public class Test
{
public Test(string dateTime)
{
this.Data = dateTime;
}
public string Data { get; set; }
}
回答by Kcvin
You give your DataTemplatea Keyso you can use explicitly define your template and reuse your template. You also need to make sure the ItemsControlis a child of the control which loads the dictionary.
你给你DataTemplate一个,Key这样你就可以使用明确定义你的模板并重用你的模板。您还需要确保ItemsControl是加载字典的控件的子项。
<DataTemplate x:Key="ADataTemplate">
<TextBlock Text="{Binding Data}" />
<Rectangle Fill="Red" Height="10" Width="10"/>
</DataTemplate>
<ListBox ItemsSource="{Binding YourItems}"
ItemTemplate="{StaticResource ADataTemplate}" />
Note: You can use implicit styling on ListBox, however that would apply the same style to all of your ListBoxes.
注意:您可以在 上使用隐式样式ListBox,但是这会将相同的样式应用于您的所有ListBoxes。
回答by James Lucas
Declare your data template in the Resources section of the current Window/UserControl etc as follows and then reference via static resource declaration:
在当前窗口/用户控件等的资源部分声明您的数据模板如下,然后通过静态资源声明引用:
<Window.Resources> For example...
<DataTemplate x:Key="MyTemplate">
<TextBlock Text="{Binding Data}" />
<Rectangle Fill="Red" Height="10" Width="10"/>
</DataTemplate>
</Window.Resources>
<ListBox ItemTemplate="{StaticResource MyTemplate}" />

