Silverlight 组合框项目模板卡住显示第一个选定项目
我有一个组合框,其 ItemTemplate 绑定到包含我的自定义标签控件之一的 DataTemplate。自定义控件所做的只是本地化分配给它的内容。
组合框(关闭时)将显示所选第一个项目的文本。但是,当所选项目发生更改时,关闭的组合框的显示将不会更新。我知道实际选定的项目已更新,因为它绑定到正确更改的属性。唯一的问题是显示文本。
因此,例如,如果我选择带有文本“Item 1”的项目,关闭的组合框将显示“Item 1”。然后,如果我选择“项目 2”,关闭的组合框仍将显示“项目 1”。
下面是它的设置方式(“Name”是 ItemsSource 中绑定的项目的属性):
<Grid.Resources>
<DataTemplate x:Key="MyTemplate">
<MyCustomLabel Content="{Binding Name}" />
<DataTemplate>
</Grid.Resources>
<Combobox ItemsSource="{Binding MyItems}" ItemTemplate="{StaticResource MyTemplate}" />
下面是我的标签控件的代码:
public class MyLabel : Label
{
/// <summary>
/// When reassigning content in the OnContentChanged method, this will prevent an infinite loop.
/// </summary>
private bool _overrideOnContentChanged;
protected override void OnContentChanged(object oldContent, object newContent)
{
// if this method has been called recursively (since this method assigns content)
// break out to avoid an infinite loop
if (_overrideOnContentChanged)
{
_overrideOnContentChanged = false;
return;
}
base.OnContentChanged(oldContent, newContent);
var newContentString = newContent as string;
if (newContentString != null)
{
// convert the string using localization
newContentString = LocalizationConverter.Convert(newContentString);
// override the content changed method
// will prevent infinite looping when this method causes itself to be called again
_overrideOnContentChanged = true;
Content = newContentString;
}
}
}
任何建议将不胜感激。谢谢!
I've got a Combobox whose ItemTemplate is bound to a DataTemplate containing one of my custom label controls. All the custom control does is localize the content assigned to it.
The Combobox (when closed) will display the text of the first item selected. However when the selected item is changed, the display of the closed Combobox will not update. I know the actual selected item is updated because it's bound to a property that changes correctly. The only problem is the display text.
So for instance if I select the item with text 'Item 1' the closed Combobox will display 'Item 1'. Then if I select 'Item 2' the closed Combobox will still display 'Item 1'.
Here's how it's set up ('Name' is a property of the items being bound in the ItemsSource):
<Grid.Resources>
<DataTemplate x:Key="MyTemplate">
<MyCustomLabel Content="{Binding Name}" />
<DataTemplate>
</Grid.Resources>
<Combobox ItemsSource="{Binding MyItems}" ItemTemplate="{StaticResource MyTemplate}" />
Below is the code for my label control:
public class MyLabel : Label
{
/// <summary>
/// When reassigning content in the OnContentChanged method, this will prevent an infinite loop.
/// </summary>
private bool _overrideOnContentChanged;
protected override void OnContentChanged(object oldContent, object newContent)
{
// if this method has been called recursively (since this method assigns content)
// break out to avoid an infinite loop
if (_overrideOnContentChanged)
{
_overrideOnContentChanged = false;
return;
}
base.OnContentChanged(oldContent, newContent);
var newContentString = newContent as string;
if (newContentString != null)
{
// convert the string using localization
newContentString = LocalizationConverter.Convert(newContentString);
// override the content changed method
// will prevent infinite looping when this method causes itself to be called again
_overrideOnContentChanged = true;
Content = newContentString;
}
}
}
Any advice would be greatly appreciated. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对组合框的 SelectedItem 属性执行 2 路数据绑定。
您将组合框绑定到的目标属性 - 应引发 PropertyChanged 事件。
代码示例可能并不“完整”——现在是凌晨 1 点,我有点累了,但它应该能让您走上正轨。
编辑2。
刚刚注意到你的模板,它是错误的,整个想法都是错误的。
请注意,texblock 的文本是没有路径的绑定(并且它也是默认绑定)。无路径绑定意味着该文本块将绑定到直接“下方”的任何内容。
Perform a 2-way databinding on the SelectedItem property of the combo box.
The target property that you're binding the combo-box to - should raise a PropertyChanged event.
The code sample might not be "complete" - it's 1am and I'm kinda tired, but it should put you on the right track.
Edit2.
Just noticed your template, it's wrong, the whole idea is wrong.
Notice that the texblock's text is a binding with no path, (and it's a default binding too.) the no-path binding means that this textblock will bind to whatever is directly "underneath".