简体   繁体   English

如何在XAML中设置用户控件的列表项

[英]How to Set List Items of a usercontrol in XAML

I create a User Control. 我创建一个用户控件。 this has a dependencypoperty by this type 这种类型具有依赖性popperty

List<CustomSubMenuItem>

and CustomSubMeuItem 和CustomSubMeuItem

class CustomSubMenuItem
{
public string Title {get;set;}
public Color BackColor {get;set;}
publiv Visibility ItemVisibility {get;set;}
public ICommand Command {get;set;}
}

in XAML i bind to the prperty in usuall. 在XAML中,我通常绑定到属性。 but i cant make this items in XAML, Like ContextMenu or ListBoxItems. 但我无法在XAML中制作此项目,例如ContextMenu或ListBoxItems。

<MyControl>
<MyControl.Items>
<CustomSubMenuItem Title="First" Visibility="{Binding Model.firstvisibility}"/>
<CustomSubMenuItem Title="Second" Visibility="{Binding Model.secondvisibility}"/>
</MyControl.Items>
</MyControl>

but this kind has error,what can I do. 但是这种错误,我该怎么办。

UPDATE: Thanks, I Reach to answer below. 更新:谢谢,我可以在下面回答。 i did not define the namespace of class in the xaml. 我没有在xaml中定义类的名称空间。 and the upper code is true when i add the namespace: before CustomSubItemMenu . 当我添加名称空间时,上部代码为true CustomSubItemMenu之前。

Set your control datacontext to your list and then bind ItemsSource to it: 将控件数据上下文设置为列表,然后将ItemsSource绑定到列表:

List<CustomSubMenuItem> MenuItems = new List<CustomSubMenuItem>();
MyControl.DataContext = MenuItems;

Then in XAML fe: 然后在XAML fe中:

  <MyControl ItemsSource="{Binding}">
    <MyControl.ItemTemplate>
        <DataTemplate><TextBlock Text="{Binding Path=Title}"/></DataTemplate>
    </MyControl.ItemTemplate>
 </MyControl>

You can bind your Title and Color the way you want it. 您可以按照需要的方式绑定标题和颜色。

UPDATE: 更新:

If you want to bind Visibility to one of your property, one way to do it is to have bool value in Model and bind it to visibility. 如果要将可见性绑定到某个属性,一种方法是在模型中具有布尔值并将其绑定到可见性。 Also you need a ValueConverter to set true value as visible and false as hidden. 另外,您还需要一个ValueConverter来将true值设置为可见,将false设置为隐藏。

First, add a namespace in window. 首先,在窗口中添加一个名称空间。 Declare that namespace where your ValueConverter class is defined. 声明用于定义ValueConverter类的名称空间。

xmlns:vm="clr-namespace:NamespaceHere"

XAML for binding visibility: XAML具有绑定可见性:

<MyControl Visibility="{Binding VisibilityValue, Converter={StaticResource converter}}"/>

Then add ValueConverter to your : 然后将ValueConverter添加到您的:

<vm:BoolToVisibilityConverter x:Key="converter" />

Lastly, you need to create the ValueConverter class, use mine as a example: 最后,您需要创建ValueConverter类,以我的示例为例:

 public class BoolToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType,
        object parameter, CultureInfo culture)
    {
        bool val = (bool)value;
        if(val)
        {
            return Visibility.Visible;
        }
        else
        {
           return Visibility.Hidden;
        }
    }

    public object ConvertBack(object value, Type targetType,
        object parameter, CultureInfo culture)
    {
        bool val = (bool)value;
        if (val)
        {
            return Visibility.Visible;
        }
        else
        {
            return Visibility.Hidden;
        }
    }
}

Your CustomSubMenuItem should derive from MenuItem or at least from FrameworkElement, if you want to instantiate it in your VisualTree, which is where <MyControl.Items> is. 如果要在VisualTree( <MyControl.Items>所在的位置)中实例化CustomSubMenuItem,则应从MenuItem或至少从FrameworkElement派生。

Alternatively, you can create your item-list as a StaticResource and then bind to this resource, like this (adjust namespaces of course) and if you need apply an ItemTemplate: 另外,您可以将您的项目列表创建为StaticResource,然后绑定到该资源,就像这样(当然要调整名称空间),如果需要,请应用ItemTemplate:

<Window.Resources>
    <x:Array x:Key="menuItems" Type="{x:Type local:CustomSubMenuItem}" 
            xmlns:local="clr-namespace:yourNamespace">
            <local:CustomSubMenuItem Property1="value1" Property2="value2" />
            <local:CustomSubMenuItem Property1="value3" />
    </x:Array>
<Window.Resources>

<MyControl ItemsSource="{StaticResource menuItems}">
     <MyControl.ItemTemplate>
           <DataTemplate>
               <TextBlock Text="{Binding Property1}" />
           </DataTemplate>
     </ListBox.ItemTemplate>
</ListBox>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从XAML设置WPF usercontrol属性? - How to set a WPF usercontrol property from XAML? 如何实例化UserControl并将项目添加到XAML中的容器 - How to instantiate UserControl and add Items to its Container in XAML 我想通过 XAML 代码中的属性在其中插入另一个 UserControl 的项目和 contentControl 的用户控件如何? - How have a UserControl with items and a contentControl in which I want to insert another UserControl by a property in XAML code? WPF绑定:如何在UserControl XAML中设置绑定源 - WPF Binding: How to set Binding Source inside UserControl XAML 如何在XAML中将datacontext设置为用户控件的类? - How do I set the datacontext to a class for a usercontrol in XAML? 如何通过绑定为XAML中的Windows窗体用户控件设置值? - How to set a value through binding for a Windows Forms UserControl in XAML? 如何在网格中放置一个 XAML 用户控件 - How to place an XAML usercontrol in a grid 声明的XAML列表项何时设置了依赖项属性? - When do declared XAML list items have their dependency properties set? 如何在初始化期间访问UserControl的XAML设置属性? - How can you access XAML-set properties of a UserControl during initialization? 如何将标签索引设置为xaml树视图项? - how to set tab index to xaml tree view items?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM