【问题标题】:Different UI for the SelectedItem and the ItemTemplate for Silverlight ComboBoxSilverlight ComboBox 的 SelectedItem 和 ItemTemplate 的不同 UI
【发布时间】:2009-06-19 03:09:31
【问题描述】:

基本上我只是在 SL3 ComboBox 中寻找“SelectedItemTemplate”。不幸的是,那不存在。

我想要的是 SelectedItem 看起来像这样:值

下拉框中的项目看起来像这样:值 + 额外信息

后者很容易通过使用 ItemTemplate 完成,但 SelectedItem 看起来也是如此。我该如何预防/解决这个问题?

【问题讨论】:

    标签: c# xaml silverlight-3.0


    【解决方案1】:

    您可以通过创建自己的 SelectionBoxItemTemplate 附加属性,然后为 ComboBox 定义一个新的样式/控件模板,该模板在内容呈现器中为选择框区域使用该模板。

    这是一个合适的附加属性:

    public class ComboBoxExt
    {
        public static DataTemplate GetSelectionBoxItemTemplate(DependencyObject obj)
        {
            return (DataTemplate) obj.GetValue(SelectionBoxItemTemplateProperty);
        }
    
        public static void SetSelectionBoxItemTemplate(DependencyObject obj, DataTemplate value)
        {
            obj.SetValue(SelectionBoxItemTemplateProperty, value);
        }
    
        public static readonly DependencyProperty SelectionBoxItemTemplateProperty =
            DependencyProperty.RegisterAttached("SelectionBoxItemTemplate", typeof (DataTemplate), typeof (ComboBoxExt),
                                                new PropertyMetadata(null));
    
    }
    

    要更新 ComboBox 控件模板,请在名为 ContentPresenterBorder 的元素中查找名为 ContentPresenter 的元素(您可以找到 ComboBox here 的默认样式)。您需要删除 ContentPresenter 的名称(否则 ComboBox 将通过代码显式设置其属性的值,而忽略您设置的数据绑定)。

    调整后的控件模板中的 ContentPresenter 元素应如下所示:

    <ContentPresenter Margin="{TemplateBinding Padding}" 
                      Content="{Binding Path=SelectedItem, RelativeSource={RelativeSource TemplatedParent}}"
                      ContentTemplate="{Binding (a:ComboBoxExt.SelectionBoxItemTemplate), RelativeSource={RelativeSource TemplatedParent}}"
                      HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
    </ContentPresenter>
    

    最后,要使用它,您可以执行以下操作:

    <ComboBox 
       Style="{StaticResource MyAdjustedComboBoxStyle}"
       ItemTemplate="{StaticResource MyDropDownAreaTemplate}"
       Behaviors:ComboBoxExt.SelectionBoxItemTemplate="{StaticResource MySelectionAreaTemplate}">
    

    【讨论】:

    • 此解决方案值得 +50。我刚刚在一个 SL 项目中遇到了这个问题,认为这是一个长期的目标,但它就像一个魅力!
    【解决方案2】:

    你在找.SelectionBoxItemTemplate吗?

    【讨论】:

    • 是的,类似的,但 SL3 ComboBox 似乎没有那个属性?
    • 奇怪...我没有 SL3 在这里进行测试,但它在 MSDN 文档中列出(即使 SL3 未在“其他版本”框中列出:添加(VS. 96)在我的链接中的 .aspx 之前)。也许这是测试版的问题?尝试忽略智能感知,如果这就是您要执行的操作,并且无论如何都使用它进行编译。
    • 唉...我试过了,得到了这个:XamlParseException。 AG_E_UNKNOWN_ERROR [Line: 96 Position: 135] 是的,第 96 行是 SelectionBoxItemTemplate 的行 ;-)
    • UNKNOWN_ERROR 向我暗示这是一个 Beta 错误。我猜你得等他们修好?
    【解决方案3】:

    SelectionBoxItemTemplate 在 Silverlight4 中可用,但无法从代码隐藏设置此属性的值,因为它是只读属性。它也不是依赖属性,因此无法使用comboBox.SetValue() 方法设置值。关于如何在代码隐藏中为该属性赋值的任何想法?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-24
      • 1970-01-01
      • 1970-01-01
      • 2011-08-17
      • 1970-01-01
      • 2014-07-27
      相关资源
      最近更新 更多