【问题标题】:Silverlight Combobox ItemTemplate Stuck Displaying First Selected ItemSilverlight 组合框项目模板卡在显示第一个选定项目
【发布时间】:2011-01-18 18:15:52
【问题描述】:

我有一个 Combobox,它的 ItemTemplate 绑定到包含我的自定义标签控件之一的 DataTemplate。自定义控件所做的只是本地化分配给它的内容。

组合框(关闭时)将显示所选第一个项目的文本。但是,当所选项目更改时,闭合组合框的显示不会更新。我知道实际选定的项目已更新,因为它绑定到正确更改的属性。唯一的问题是显示文本。

例如,如果我选择带有文本“项目 1”的项目,关闭的组合框将显示“项目 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;
        }
    }
}

任何建议将不胜感激。谢谢!

【问题讨论】:

  • 这不是应该在组合框中更改的标签。组合框将为 ItemsSource 中的每个项目生成一个标签。

标签: silverlight data-binding datatemplate itemtemplate


【解决方案1】:

对组合框的 SelectedItem 属性执行 2 路数据绑定。

您将组合框绑定到的目标属性 - 应该引发 PropertyChanged 事件。

<ComboBox ItemsSource={Binding Path=Source} SelectedItem={Binding Path=CurrentItem, Mode=TwoWay}/>


class ComboContext : INotifyPropertyChanged
{
public List<object> Source { get; set; } // attach your source here
public object CurrentItem { get { return mCurrentItem; } set { mCurrentItem = value; OnPropertyChanged("CurrentItem"); } } // bind to this property
private object mCurrentItem;

void OnPropertyChanged(string name)
{
if(PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}

代码示例可能不“完整” - 现在是凌晨 1 点,我有点累,但它应该能让你走上正轨。

编辑2。

刚刚注意到你的模板,它是错误的,整个想法都是错误的。

<ComboBox ItemsSource={Binding Path=Source} SelectedItem={Binding Path=CurrentItem, Mode=TwoWay}>
    <ComboBox.ItemTemplate>
        <DataTemplate>
        <Grid>
            <TextBlock Text={Binding}/>
        </Grid>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

请注意,texblock 的文本是一个没有路径的绑定,(它也是一个默认绑定。)无路径绑定意味着这个文本块将绑定到直接“下方”的任何内容。

【讨论】:

  • 两件事。一:绑定ComboBox的SelectedItem属性没有问题。我留下了那部分以保持示例整洁。二:我的示例适用于普通的 Label 控件。只有当我使用自定义标签控件时才会遇到问题。我想知道为什么它只在我使用自定义控件时才会失败。感谢您的回复。
猜你喜欢
  • 1970-01-01
  • 2015-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多