用户控件不同类型的WPF名单控件、不同类型、名单、用户

2023-09-04 01:19:52 作者:胭脂ㄨ The death of love

我有一个WPF列表框,里面叫洁悠神。一个用户控件

这伟大工程,因为我很新的WPF这已经是非常IM pressive。我想现在要做的就是在基于绑定属性列表中不同的用户控件。

这可能吗?如果不是这样,我应该怎么回事实现这一目标?

我使用一个名单,因为我想允许用户控制滴/拖拽排序,并且会有一个可变号码,以便似乎是有道理的 - 各种途径,欢迎

 < ListBox的X:名称=peopleListBox
    的Horizo​​ntalAlignment =拉伸
    VerticalAlignment =拉伸
    ItemContainerStyle ={的StaticResource ListBoxItemStretch}
    前景=透明
    BorderBrush =透明
    背景=透明
    Grid.ColumnSpan =2的SelectionChanged =peopleListBox_SelectionChanged>
        < ListBox.ItemTemplate>
            <的DataTemplate>
                <电网>
                     <我:洁悠神保证金=4>< /我:洁悠神>
                < /网格>
            < / DataTemplate中>
        < /ListBox.ItemTemplate>
    < /列表框>
 

解决方案 对wpf 的入门记录总结 用户控件和自定义控件

您可以使用的 DataTemplateSelector ,在 SelectTemplate()方法,你可以检查哪些DataTemplate中,以用于项目公司目前通过:

在XAML:

 <! - 定义资源模板
     ChartDataTemplate是一个ChartDataTemplate.xaml,相同的其他
 - >
< UserControl.Resources>
     < D​​ataTemplate中X:关键=ChartDataTemplate>
          <的观点:LineChartView />
     < / DataTemplate中>

     < D​​ataTemplate中X:关键=GridDataTemplate>
         <的观点:PieChartView />
     < / DataTemplate中>
< /UserControl.Resources>

<! -  ListView控件的ItemTemplate应指向模板选择 - >
< ItemsControl.ItemTemplate>
  <的DataTemplate>
      <内容presenter
             ContentTemplateSelector ={的StaticResource MyTemplateSelector}>
 

在$后面C $ C:

专用密封类MyTemplateSelector:DataTemplateSelector  {     公众覆盖的DataTemplate SelectTemplate(                                       对象的项目,                                       DependencyObject的容器)     {         // 1.案例项目的对象是绑定到每个ListView项         // 2.基于对象类型/状态返回正确的DataTemplate         //为this.Resources [ChartDataTemplate]或         // this.Resources [GridDataTemplate]     }   }

I have a WPF listbox that houses a user control called JUC.

This works great and as I'm very new to WPF this already is very impressive. What I would like to do now is have different user controls in the list based upon a bound property.

Is this possible? If not, how else should I achieve this?

I'm using a list because I want to allow drop/drag ordering of the user controls, and there will be a variable number so seems to make sense - alternative approaches are welcome.

<ListBox x:Name="peopleListBox" 
    HorizontalAlignment="Stretch" 
    VerticalAlignment="Stretch"
    ItemContainerStyle="{StaticResource ListBoxItemStretch}"
    Foreground="Transparent" 
    BorderBrush="Transparent" 
    Background="Transparent" 
    Grid.ColumnSpan="2" SelectionChanged="peopleListBox_SelectionChanged">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid>
                     <my:JUC Margin="4"></my:JUC>
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

解决方案

You can use DataTemplateSelector, in SelectTemplate() method you can check which DataTemplate to use for currently passed in item:

In XAML:

<!-- define templates in resources
     ChartDataTemplate is a ChartDataTemplate.xaml, the same for other
-->
<UserControl.Resources>
     <DataTemplate x:Key="ChartDataTemplate">
          <views:LineChartView />
     </DataTemplate>

     <DataTemplate x:Key="GridDataTemplate">
         <views:PieChartView />
     </DataTemplate>
</UserControl.Resources>

<!-- ListView Itemtemplate should point to template selector -->
<ItemsControl.ItemTemplate>     
  <DataTemplate>
      <ContentPresenter 
             ContentTemplateSelector = "{StaticResource MyTemplateSelector}">

In Code behind:

 private sealed class MyTemplateSelector: DataTemplateSelector
 { 

    public override DataTemplate SelectTemplate(
                                      object item, 
                                      DependencyObject container)
    {
        // 1. case item to your object which is bound to each ListView item
        // 2. based on object type/state return correct DataTemplate
        // as this.Resources["ChartDataTemplate"] or
        // this.Resources["GridDataTemplate"] 
    }
  }