Silverlight列表框中的ItemsSource绑定问题
我试图在Silverlight列表框中列出一些字符串。我正在将一个普通的Silverlight列表框中的ItemsSource绑定问题,silverlight,c#-4.0,silverlight-4.0,Silverlight,C# 4.0,Silverlight 4.0,我试图在Silverlight列表框中列出一些字符串。我正在将一个普通的列表绑定到项资源,然后指定列表项的属性以显示在显示成员路径中。有一种特定于我的实现的东西导致ListBox显示模板化的项,而不是在这些项中指定的属性 下面是一个场景。我有一个从UserControl派生的父类,它添加了一个“Title”依赖属性。我创建了几个从父控件派生的子控件,并指定继承的Title属性。出于某种原因,绑定到ListBox中的Title属性会导致意外行为。代码如下: public class Parent
列表
绑定到项资源
,然后指定列表项的属性以显示在显示成员路径
中。有一种特定于我的实现的东西导致ListBox显示模板化的项,而不是在这些项中指定的属性
下面是一个场景。我有一个从UserControl
派生的父类,它添加了一个“Title”依赖属性。我创建了几个从父控件派生的子控件,并指定继承的Title属性。出于某种原因,绑定到ListBox中的Title属性会导致意外行为。代码如下:
public class Parent : UserControl
{
public string Title
{
get { return (string)GetValue(TitleProperty); }
set { SetValue(TitleProperty, value); }
}
// Using a DependencyProperty as the backing store for Title. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TitleProperty =
DependencyProperty.Register("Title", typeof(string), typeof(Parent), new PropertyMetadata(String.Empty));
}
子XAML代码(Child1和Child2基本上是相同的XAML,有一些无关紧要的代码落后)
“视图模型”
public类视图模型:InotifyProperty已更改
{
公共列表{get;set;}
公共视图模型()
{
婴儿=新列表();
Child1 baby1=新的Child1();
Child2 baby2=新的Child2();
婴儿。添加(婴儿1);
添加(baby2);
}
公共事件属性更改事件处理程序属性更改;
私有void OnPropertyChanged(字符串propertyName)
{
if(PropertyChanged!=null)
调用(这是新的PropertyChangedEventArgs(propertyName));
}
}
MainPage.xaml
<UserControl x:Class="TemplateBindingTest.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" xmlns:my="clr-namespace:TemplateBindingTest" Height="268" Width="355">
<Grid x:Name="LayoutRoot" Background="White">
<ListBox ItemsSource="{Binding Path=Babies}" DisplayMemberPath="Title" Height="219" HorizontalAlignment="Left" Margin="12,12,0,0" VerticalAlignment="Top" Width="180" />
<my:Child1 HorizontalAlignment="Left" Margin="203,26,0,0" x:Name="child1" VerticalAlignment="Top" />
<my:Child2 HorizontalAlignment="Left" Margin="203,92,0,0" x:Name="child2" VerticalAlignment="Top" />
</Grid>
因此,忽略在“viewmodel”类中维护UI控件列表有点奇怪这一事实,这一切都是相当简单的Silverlight。在主页上的ListBox控件中,我希望看到每个子控件的标题。相反,子控件本身显示在列表框中。我错过了什么?我觉得很奇怪,Silverlight决定在调试输出或其他错误消息中没有抱怨地呈现子控件。这就像DisplayMemberPath
属性被完全忽略一样。这可能是Silverlight中的一只虫子吗
为了便于测试,这里为包含上述代码的完整Visual Studio项目干杯。这种行为似乎是出于设计。如果列表框看到内容是
UIElement
的派生,那么它会做出一个简单(我认为合理)的假设,即您希望显示该内容
你是对的,你所做的是“有点奇怪”,你正在为此付出代价。这可能为您提供了一个解决方案。但是,我建议您查看在viewmodel中保存
UserControl
实例的选项 这种行为似乎是故意的。如果列表框看到内容是UIElement
的派生,那么它会做出一个简单(我认为合理)的假设,即您希望显示该内容
你是对的,你所做的是“有点奇怪”,你正在为此付出代价。这可能为您提供了一个解决方案。但是,我建议您查看在viewmodel中保存
UserControl
实例的选项 @AnthonyWJones是正确的,他提供了这个答案:我需要实现我自己的ListBox黑客版本,以实现我问题中提到的所需功能
public class MyListBox : ListBox
{
protected override bool IsItemItsOwnContainerOverride(object item)
{
return false;
}
protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
{
base.PrepareContainerForItemOverride(element, item);
((ListBoxItem)element).ContentTemplate = ItemTemplate;
}
}
我想这个故事的寓意是不要试图在ViewModel类中直接处理UI元素。这不是最佳实践(当然也不是MVVM),这是有原因的。@AnthonyWJones是正确的,他给出了这个答案:我需要实现我自己的列表框黑客版本,以实现我问题中提到的所需功能
public class MyListBox : ListBox
{
protected override bool IsItemItsOwnContainerOverride(object item)
{
return false;
}
protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
{
base.PrepareContainerForItemOverride(element, item);
((ListBoxItem)element).ContentTemplate = ItemTemplate;
}
}
我想这个故事的寓意是不要试图在ViewModel类中直接处理UI元素。出于某种原因,这不是最佳实践(当然也不是MVVM)
public class MyListBox : ListBox
{
protected override bool IsItemItsOwnContainerOverride(object item)
{
return false;
}
protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
{
base.PrepareContainerForItemOverride(element, item);
((ListBoxItem)element).ContentTemplate = ItemTemplate;
}
}