陶氏如何为Combobox创建一个Itemtemplate? [英] How Dow I Create An Itemtemplate For Combobox?

查看:65
本文介绍了陶氏如何为Combobox创建一个Itemtemplate?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


你好。我有以下XAML代码:



hi there. i have following XAML code:

<ComboBox x:Name="MainComboBox" Height="35" Width="250" removed="WhiteSmoke" BorderBrush="Gray"

                  IsEditable="True" TextSearch.TextPath="Name" TextBoxBase.TextChanged="MainComboBox_TextChanged">
            <ComboBox.Resources>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
                <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent"/>
            </ComboBox.Resources>
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <TreeView BorderBrush="Transparent" BorderThickness="0" ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type ComboBox}, Mode=FindAncestor}, Path=ItemsSource}">
                        <TreeView.ItemTemplate>
                            <HierarchicalDataTemplate ItemsSource="{Binding Children}" DataType="{x:Type data:SubscriberGroupModel}">
                                <CheckBox Content="{Binding Name}" Height="25" Width="80" />
                            </HierarchicalDataTemplate>
                        </TreeView.ItemTemplate>
                    </TreeView>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>





和我的代码背后:



and my code behind:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    List<SubscriberGroupModel> list = SubscriberGroupDB.GetCustomSubscriberGroup();
    MainComboBox.ItemsSource = list;
}



此控件无法正常工作,因为它会多次生成itms。

我的查询s正确。

请帮帮我。

非常感谢


this control doesn't work properly because it produces itms more than once.
my query s correct.
please help me.
thanks alot

推荐答案

嗨阿里,请按照以下步骤操作。我在我身边查看它并且它有效..



1)添加 Childern 作为 SubscriberGroupModel class。这意味着 -



Hey Ali, Please follow these steps. I check this on my side and it works..

1) Add Childern as one more property in SubscriberGroupModel class. It means -

List<Subscribergroupmodel> childern;
public List<Subscribergroupmodel> Childern
{
  get { return childern; }
  set
  {
      childern = value;
      NotifyPropertyChanged("Childern");
   }
}





2)之后更改Loaded方法 - (Window_Loaded)< br $>




2)After that change the Loaded method - (Window_Loaded)

private void Window_Loaded(object sender, RoutedEventArgs e)
 {
    List<Subscribergroupmodel> list = SubscriberGroupDB.GetCustomSubscriberGroup();
    SubscriberGroupModel sgmobj = new SubscriberGroupModel();
    sgmobj.Childern = list;

    List<Subscribergroupmodel> obj = new List<Subscribergroupmodel>();
    obj.Add(sgmobj);

    MainComboBox.ItemsSource = obj;

  }





3)最后改变你的XAML -





3) And Lastly change in your XAML -

<ComboBox x:Name="MainComboBox" TextBoxBase.TextChanged="MainComboBox_TextChanged">           
     <ComboBox.ItemTemplate>
         <DataTemplate>
             <TreeView BorderBrush="Transparent" BorderThickness="0" ItemsSource="{Binding Childern}">
                  <TreeView.ItemTemplate>
                       <HierarchicalDataTemplate >
                            <CheckBox Content="{Binding Name}" Height="25" Width="80" />
                        </HierarchicalDataTemplate>
                    </TreeView.ItemTemplate>
             </TreeView>
          </DataTemplate>
      </ComboBox.ItemTemplate>
  </ComboBox>

< br $> b $ b



我希望这能解决你的问题......




I hope this will solve your problem...


这篇关于陶氏如何为Combobox创建一个Itemtemplate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆