【问题标题】:How to modify silverlight combobox data display如何修改silverlight组合框数据显示
【发布时间】:2010-07-27 20:37:20
【问题描述】:

我有一个组合框定义如下:

<ComboBox x:Name="cboDept" Grid.Row="0" Margin="8,8,8,8" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120" 
                  ItemsSource="{Binding Source={StaticResource cvsCategories}}">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Vertical" Width="Auto" Height="Auto">
                        <sdk:Label Content="{Binding CategoryID}" Height="20" />
                        <sdk:Label Content="{Binding CategoryName}" Height="20" />
                    </StackPanel>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>

它工作正常。但是,一旦我在列表中选择了一个项目,我希望将不同的模板应用于显示给用户的组合框选定项目(弹出消失后显示的项目)。在上述情况下,一旦我选择了相应的项目,我希望只在 ComboBox 中显示 CategoryName。

谁能告诉我如何实现这一目标?

谢谢

【问题讨论】:

    标签: silverlight silverlight-4.0


    【解决方案1】:

    您需要自己创建一个包含一些已定义模板的 ResourceDictionary。在下面,ComboBoxTemplateOne 和 ComboBoxTeplateTwo 是用户控件,用于在您想要的庄园中显示组合框。

       <UserControl.Resources>
            <ResourceDictionary>
              <DataTemplate x:Key="TemplateOne">
                   <local:ComboBoxTemplateOne />
                </DataTemplate>
              <DataTemplate x:Key="TemplateTwo">
                   <local:ComboBoxTemplateTwo />
                </DataTemplate>
            </ResourceDictionary>
       </UserControl.Resources>
    

    然后您需要创建自己的类,该类继承自 ContentControl“DataTemplateSelector”,覆盖 OnContentChanged

      Protected Overrides Sub OnContentChanged(ByVal oldContent As Object, ByVal newContent As Object)
        MyBase.OnContentChanged(oldContent, newContent)
    
        Me.ContentTemplate = SelectTemplate(newContent, Me)
    
      End Sub
    

    然后,您需要创建另一个继承自上述 DataTemplateSelector 的类,该类覆盖 SelectTemplate(“TemplateSelectorClass”),它将返回上面定义的 DataTemplate(“TemplateOne”或“TemplateTwo”)。 同样在这个派生类中,您需要为您拥有的每个模板定义一个属性

    Public Property ComboboxTemplateOne As DataTemplate
    

    然后回到你的 XAML 和 n 打击 XAML

     <local:TemplateSelectorClass  ComboboxTemplateOne="{StaticResource TemplateOne}"  Content="{Binding Path=ActiveWorkspace}>
    

    这应该可以工作,因为它有效地完成了与在 WPF 中设置“DataTemplate”属性相同的工作(在 SilverLight 中不存在) 我意识到这里有相当多的步骤,而且非常繁琐,但希望这能让你到达那里。有任何问题,请大声疾呼。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多