[英]WPF DataTemplate Binding to Content without Path - is this a bug?
I have a problem understanding the following behavior (tested in .Net 4.0) 我在理解以下行为时遇到问题(在.Net 4.0中测试)
First: The following example works as I expected: It shows a CheckBox inside a Button. 首先:下面的示例按我的预期工作:它在Button中显示一个CheckBox。
C#: C#:
DataContext = new CheckBox();
XAML: XAML:
<Button Content="{Binding}"/>
Inside an ItemsControl with a Path ("MyProperty"), it works too: 在带有路径的ItemsControl(“ MyProperty”)中,它也可以工作:
C#: C#:
DataContext = new { MyList = new List<object>() { new { MyProperty = new CheckBox() } } };
XAML: XAML:
<ItemsControl ItemsSource="{Binding Path=MyList}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Content="{Binding Path=MyProperty}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
But inside an ItemsControl without a Path, it replaces the Button and only shows the CheckBox: 但是在没有路径的ItemsControl内部,它将替换Button并仅显示CheckBox:
C#: C#:
DataContext = new { MyList = new List<CheckBox>() { new CheckBox() } };
XAML: XAML:
<ItemsControl ItemsSource="{Binding Path=MyList}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Content="{Binding}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Why this example doesn't work?? 为什么这个例子不起作用? Is this a bug in WPF?
这是WPF中的错误吗? Thanks a lot for helping!
非常感谢您的帮助!
This is not a bug. 这不是错误。 The
DataTemplate
isn't applied to ContentControls
. DataTemplate
不适用于ContentControls
。 That's why your sample code doesn't work when the DataContext
of an item in the ItemsControl
is a CheckBox
but it does work when the DataContext
is an object
. 这就是为什么您的示例代码在
ItemsControl
中的项目的DataContext
是CheckBox
时不起作用,而在DataContext
是object
时却起作用的原因。
You are not supposed to define user interface elements in the DataContext
/view model. 您不应在
DataContext
/ view模型中定义用户界面元素。
I wouldn't consider the described behavior as WPF/XAML-bug. 我不会将描述的行为视为WPF / XAML错误。
ItemsControl (compared to ListView or ListBox) has no default ItemContainer (as ListViewItem or ListboxItem). ItemsControl(与ListView或ListBox相比)没有默认的ItemContainer(作为ListViewItem或ListboxItem)。 With ListBox or ListView your example will work.
使用ListBox或ListView,您的示例将起作用。
If you have a List<Textbox>
as ItemsSource
, then ItemsControl
uses items from ItemsSource
as ItemContainers so TextBoxes will be ItemContainers and you become 如果您有一个
List<Textbox>
作为ItemsSource
,则ItemsControl
将ItemsSource
项目用作ItemContainers,因此TextBoxes将是ItemContainers,您将成为
System.Windows.Data Error: 26 : ItemTemplate and ItemTemplateSelector are ignored for items already of the ItemsControl's container type; Type='CheckBox'
in your debug output so you DataTemplate will be ignored(without specifying ItemTemplate you will have the same issue). 在调试输出中,因此您将忽略DataTemplate(如果不指定ItemTemplate,则会遇到相同的问题)。
To solve it you could derive from ItemsControl
and override IsItemItsOwnContainerOverride method: 要解决此问题,您可以从
ItemsControl
派生并重写IsItemItsOwnContainerOverride方法:
Determines if the specified item is (or is eligible to be) its own container
确定指定的项目是否是(或有资格成为)自己的容器
Thanks a lot for your answers! 非常感谢你的回答!
I didn't know the method IsItemItsOwnContainerOverride. 我不知道方法IsItemItsOwnContainerOverride。 If I override it and always return true then all works as I have expected.
如果我重写它并始终返回true,那么所有工作都将如我所料。
I know, that this way of implementation is not the best one. 我知道,这种实施方法不是最佳方法。 But now I understand the behavior and I'm happy ;)
但是现在我明白了这种行为,我很高兴;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.