将 ItemsControl ItemSource 绑定到 UserControl 依赖属性 [英] Bind ItemsControl ItemSource to UserControl Dependency Property

查看:61
本文介绍了将 ItemsControl ItemSource 绑定到 UserControl 依赖属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我尝试创建具有依赖属性的用户控件的第一次尝试.所以请原谅我对这个主题缺乏了解.我在我的一个页面上创建了一个通用设计,我想将其引入可重用的用户控件.

this is my very first shot at trying to create a User Control with Dependency Properties. So excuse my lack of knowledge over the subject. I created a general design on one of my pages that I would like to bring over to a reusable user control.

页面上的原始控件

所以这是我试图移植到可重用用户控件中的控件

So this is the control I am trying to port into a reusable UserControl

<ToggleButton x:Name="filterButton" Background="{StaticResource BackgroundLightBrush}" BorderThickness="0">
    <fa:ImageAwesome x:Name="border"
                     Height="15"
                     Foreground="{StaticResource MediumBlueBrush}"
                     Icon="Filter"/>
</ToggleButton>

<Popup x:Name="popup" 
       AllowsTransparency="True"
       StaysOpen="False"
       PlacementTarget="{Binding ElementName=filterButton}"
       IsOpen="{Binding ElementName=filterButton,Path=IsChecked,Mode=TwoWay}">
    <Border BorderThickness="2" BorderBrush="{StaticResource MediumBlueBrush}" Background="{StaticResource BackgroundLightBrush}" CornerRadius="5">
        <ItemsControl ItemsSource="{Binding HeaderList}"
                      Background="{StaticResource BackgroundLightBrush}"
                      Margin="1">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal" Margin="5">
                        <CheckBox IsChecked="{Binding IsChecked}"/>
                        <TextBlock Text="{Binding HeaderName}"/>
                    </StackPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Border>
</Popup>

这是该控件的外观图片

这是我的依赖属性代码

在这里你可以看到我创建了一个 RuleColumnHeader 类型的 ObservableCollection.这是我尝试将 UserControl 设置为的项目来源.

Here you can see I created an ObservableCollection of type RulesColumnHeader. This is the item source I am trying to set my UserControl to.

public partial class FilterDropDown : UserControl
{
    public ObservableCollection<RulesColumnHeader> ItemSource
    {
        get => (ObservableCollection<RulesColumnHeader>)GetValue(ItemSourceProperty);
        set => SetValue(ItemSourceProperty, value);
    }
    // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ItemSourceProperty =
        DependencyProperty.Register("ItemSource", typeof(ObservableCollection<RulesColumnHeader>), typeof(FilterDropDown), new FrameworkPropertyMetadata(null));

    public FilterDropDown()
    {
        InitializeComponent();
    }
}

用户控件

这是我创建用户控件并将项目源绑定到我创建的依赖项属性的尝试".

Here is my "attempt" at creating a user control and binding the item source to the dependency property I created.

<UserControl x:Class="YAI.BomConfigurator.Desktop.Control.FilterDropDown"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:YAI.BomConfigurator.Desktop.Control"
         xmlns:fa="http://schemas.fontawesome.io/icons/"
         mc:Ignorable="d" 
         d:DesignHeight="50" d:DesignWidth="50">
<Grid>
    <ToggleButton x:Name="filterButton" Background="Transparent" BorderThickness="0">
        <fa:ImageAwesome x:Name="border"
                         Height="15"
                         Foreground="{StaticResource MediumBlueBrush}"
                         Icon="Filter"/>
    </ToggleButton>

    <Popup x:Name="popup" 
           AllowsTransparency="True"
           StaysOpen="False"
           PlacementTarget="{Binding ElementName=filterButton}"
           IsOpen="{Binding ElementName=filterButton,Path=IsChecked,Mode=TwoWay}">

        <Border BorderThickness="2" BorderBrush="{StaticResource MediumBlueBrush}" Background="{StaticResource BackgroundLightBrush}" CornerRadius="5">
            <ItemsControl ItemsSource="{Binding ItemSource, Source={local:FilterDropDown}}"
                          Background="{StaticResource BackgroundLightBrush}"
                          Margin="1">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal" Margin="5">
                            <CheckBox IsChecked="{Binding IsChecked}"/>
                            <TextBlock Text="{Binding HeaderName}"/>
                        </StackPanel>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </Border>
    </Popup>
</Grid>

在 ItemsControl 上的 ItemSource 绑定中,source= 部分会引发错误,指出标记扩展无效,预期类型为‘对象’,实际为 FilterDropDown".

On my ItemSource binding on ItemsControl for the, the source= portion throws an error saying "Invalid Markup Extension, expected type is 'object' actual is FilterDropDown".

所以这基本上就是我现在所处的位置.我不确定如何前进或从这里开始做什么.我想弄清楚如何将 UserControl 项源绑定到依赖项属性.我不知道是我的语法还是我做错了整件事.如果有人能帮助指导我,那就太好了.

So this is basically where I am at now. I am not sure how to move forward or what to do from here. I am trying to figure out how to bind the UserControl item source to the dependency property. I don't know if its my syntax or I am doing this whole thing incorrectly. If anyone could help guide me along that would be great.

谢谢,

推荐答案

ItemsSource Binding的Source属性设置错误.

Setting the Source property of the ItemsSource Binding is wrong.

替换

ItemsSource="{Binding ItemSource, Source={local:FilterDropDown}}"

ItemsSource="{Binding ItemSource,
                      RelativeSource={RelativeSource AncestorType=UserControl}}"

除此之外,将 ItemsSource 声明为 ObservableCollection 是不必要的,甚至是错误的.使用更通用的类型,如 IEnumerable:

Besides that, it is unnecessary or even wrong to declare the ItemsSource as an ObservableCollection. Use a more general type like IEnumerable:

public IEnumerable ItemSource
{
    get => (IEnumerable)GetValue(ItemSourceProperty);
    set => SetValue(ItemSourceProperty, value);
}

public static readonly DependencyProperty ItemSourceProperty =
    DependencyProperty.Register(
        nameof(ItemSource), typeof(IEnumerable), typeof(FilterDropDown));

这篇关于将 ItemsControl ItemSource 绑定到 UserControl 依赖属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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