【问题标题】:How to write a template for Items of an ItemsControl如何为 ItemsControl 的项目编写模板
【发布时间】:2013-10-02 17:10:06
【问题描述】:

我有一个传递给页面的 IEnumerable,我想在我的页面上显示这个列表,我可以简单地将列表框的 ItemsSource 设置为它,然后将显示一个字符串列表

但我想在每个项目旁边放置一个复选框,以使这些复选框可以选择字符串(就像您如何在 android 中将联系人添加到 SMS 一样)。我可以将两个列表框放在那里并将它们的滚动绑定到彼此,然后将其中一个的 ItemsSource 设置为 IEnumerable 并将复选框添加到每个项目的第二个列表框。但是很尴尬!

现在,首先哪个控件更适合这个任务 ItemsControl 或 ListBox(我知道 ListBox 是一个 ItemsControl)? 那么如何为listbox/Itemscontrol的项目编写这样的模板呢?

我在网上搜索过,我发现的只是数据模板的例子!但我没有任何数据要绑定!我将 IEnumerable 传递给 Page,然后将 Listbox/ItemsControl 的 Itemssource 设置为它。我希望 ItemsControl/Listbox 在每个项目旁边放置一个复选框。

【问题讨论】:

    标签: .net wpf xaml


    【解决方案1】:

    HierarchicalDataTemplate 可能是你的答案。

    我写了一个用户控件,里面只有一个Menu。为了对将在运行时插入的MenuItens 进行模板/样式化,我使用了上述HierarchicalDataTemplate

    所以,在我的<UserControl.Resources> 标签中,我插入了下面的代码。请注意,代码只是您实际可以做的小预览! (WPF 是非常可定制的!)
    代码:

    <HierarchicalDataTemplate DataType="{x:Type ViewModel:CmpMenuViewModel}" ItemsSource="{Binding MenuSubItem}" >
            <HierarchicalDataTemplate.ItemContainerStyle>
                <Style TargetType="MenuItem">
                    <Setter Property="Command" Value="{Binding Comando}" />
                    <Setter Property="CommandParameter" Value="{Binding Texto}"/>
                </Style>
            </HierarchicalDataTemplate.ItemContainerStyle>
            <StackPanel Orientation="Horizontal">
                <Image Source="{Binding Imagem}" />
                <AccessText Text="{Binding Texto}" />
            </StackPanel>
    </HierarchicalDataTemplate>
    

    &lt;StackPanel... 位是我的实际模板。
    我的风格位于&lt;HierarchicalDataTemplate.ItemContainerStyle&gt; 中。不要忘记将 &lt;Style TargetType=&gt; 更改为您的实际 TargetType!

    最好的问候,
    蒂亚戈

    【讨论】:

      【解决方案2】:

      您需要将数据包装到另一个类中,该类对您的 DataTemplate 的复选框具有可绑定属性。

      例如:

      <ItemsControl ItemsSource="{Binding YourList}">
        <ItemsControl.ItemTemplate>
          <DataTemplate>
            <CheckBox Content="{Binding TextValue}"
                      IsChecked="{Binding IsChecked}" />
          </DataTemplate>
        </ItemsControl.ItemTemplate>
      </ItemsControl>
      

      IEnumerable 中每个项目的 ViewModel 可能如下所示:

      class Checkable : ViewModelBase {
      
        private bool _isChecked;
        private string _textValue;
      
        public bool IsChecked {
            get {
              return _isChecked;
            }
            set {
              _isChecked = value;
              OnPropertyChanged("IsChecked");
            }
          }
      
        public string TextValue {
            get {
              return _textValue;
            }
            set {
              _textValue = value;
            }
          }
        }
      

      您的 UserControl 的 ViewModel 将有一个 IEnumerable,例如:

      public List<Checkable> YourList;
      

      您使用 ListBox 还是 ItemsControl 取决于您正在寻找什么类型的功能(我认为 ItemsControl 就是您在此处寻找的功能)。

      【讨论】:

        猜你喜欢
        • 2018-02-07
        • 1970-01-01
        • 2023-03-25
        • 1970-01-01
        • 1970-01-01
        • 2011-04-14
        • 1970-01-01
        • 2016-06-17
        • 2012-07-03
        相关资源
        最近更新 更多