将 ItemsControl ItemSource 绑定到 UserControl 依赖属性

2023-11-30

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

页面上的原始控件

这就是我试图移植到可重用 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>

这是该控件的外观图片

enter image description here

这是我的依赖属性代码

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

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();
    }
}

用户控制

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

<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 上的 ItemSsource 绑定上,source= 部分抛出一个错误,指出“无效的标记扩展,预期类型是‘对象’,实际是 Filter DropDown”。

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

谢谢你,


设置SourceItemsSource Binding 的属性是错误的。

Replace

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

with

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

除此之外,将 ItemsSource 声明为 ObservableCollection 是不必要的,甚至是错误的。使用更通用的类型,例如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));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将 ItemsControl ItemSource 绑定到 UserControl 依赖属性 的相关文章

随机推荐

  • 键入时即可访问带结果的多字段搜索

    大家早上好 我今天的问题是关于多领域搜索 我有一个拆分表单 字段和单个记录位于顶部 数据表视图中的所有数据位于底部 我有多个想要搜索的字段 以便用户可以根据多个条件找到特定的人 这是一位同事帮助我完成的 VBA 现在运行良好 我想添加一个b
  • Update 3 后 Visual Studio 2013 Premium 解决方案加载错误

    我安装了 VS2013Update 3 现在打开解决方案时出现错误 Microsoft VisualStudio Web Publish WebPublishPackage MircosoftVisualStudio Web Publish
  • Ruby 中的映射和收集之间的区别?

    我用谷歌搜索了这个 得到了不完整 矛盾的意见 做一个实际上有什么区别吗 map并做一个collect在 Ruby Rails 中的数组上 The docs似乎没有提出任何建议 但是方法或性能可能存在差异吗 其实没有什么区别map在 C 中实
  • Polars:通过算法取消嵌套列,无需 for 循环

    我正在使用用嵌套结构 有时是多层深度 编写的多个镶木地板数据集 我需要输出一个扁平的 无结构 模式 现在我能想到的唯一方法是使用 for 循环来迭代列 这是一个简单的例子 我在其中进行循环 while len x name for x in
  • emacs24 中的词法评估

    谁能解释一下怎么做eval与 emacs24 一起工作吗 从eval描述 eval is a built in function in C source code eval FORM optional LEXICAL Evaluate FO
  • 如何使用自定义 Dropwizard 过滤器有选择地保护资源

    我正在使用 Dropwizard 0 9 2 我想创建一个不需要 GET 身份验证而需要基本 POST 身份验证的资源 我努力了 Path protectedPing Produces MediaType TEXT PLAIN public
  • pip 缓存文件夹在哪里?

    Python pip 缓存文件夹在哪里 我在安装过程中遇到错误 现在使用缓存文件重新安装软件包 那个目录在哪里 我想对它们进行备份以便将来安装 是否可以 例如我有这个 Using cached cssselect 0 9 1 tar gz
  • 如何将 C++ 字符串转换为 int? [复制]

    这个问题在这里已经有答案了 可能的重复 C 将字符串转换为int 我让用户按顺序输入 9 个数字 我需要将字符串数字转换为 int string num int num int product 10 cout lt lt enter num
  • Magento 前端(会话)cookie

    我在我的 magento 商店的 IE 中出现一些奇怪的行为 丢失了前端 会话 cookie 有人知道在magento代码中前端cookie的设置在哪里吗 Thanks 阿法克 frontend cookie 在分派当前操作之前设置 看一下
  • VB6 中未设置变量的默认值是多少?

    我正在使用一些旧代码 它定义了一个像这样的全局变量 Public myvar 该变量从未被赋值 但稍后在测试中使用 If myvar lt gt somevalue then do something End If 为了某种价值的什么价值
  • X86 编码近调用相对偏移量

    假设我有以下一组说明 00E79E00 E8 AE580000 CALL someprocess 00E7F6B3 00E79E05 85C0 TEST EAX EAX output taken from OllyDbg 如何对近调用 0x
  • [Int] 和 Array 有什么区别?

    let numberList Array 1 10 type Array
  • 方法名称必须以 findBy 或 findOneBy 开头。 Symfony 未定义方法?

    我正在努力通过Symfony2 的第 4 部分 在更新控制器和帮助程序类代码时 我收到以下错误消息 Undefined method getLatestBlogs The method name must start with either
  • 编译时在浮点型和双精度型之间切换

    如果我想在编译时在浮点精度和双精度之间切换 我应该在哪里查看 就像 如果用户想要所有内容都是浮点数而不是双精度 我如何保持这种灵活性 换句话说 我应该如何定义一个可以有条件地浮点或双精度的变量 如果可以在编译时进行切换 一个简单的方法typ
  • 为什么内联 div 的行为与内联 span 不同

    我尝试过内联 div 但它不起作用 很难解释 请看下面的链接 http jsfiddle net CsS5v 1 p style background red SDFDSDSFDSSFDAFASasf br span style backg
  • PHP:如何重命名方法?

    PHP 5 2 中是否可以在运行时重命名类方法 是否可以使用反射来做到这一点 Given class Test public function myMethod echo in my method 我希望能够重命名myMethod to o
  • ng-include 和 ngRoute:如何让它们一起工作? (即路由到 ng-include 中的视图)

    EDITED 我的应用程序具有以下结构 索引 html
  • 如何在 C 中进行 Base64 编码(解码)?

    我在 unsigned char 变量中有二进制数据 我需要在 c 中将它们转换为 PEM base64 我查看了 openssl 库 但找不到任何函数 有谁有任何想法吗 这是我正在使用的 include
  • 静态局部变量什么时候出现?

    这本书Robert Lafore 的 C 面向对象编程 says 静态局部变量具有自动局部变量的可见性 变量 即在包含它的函数内部 然而 其 生命周期与全局变量的生命周期相同 只是它 直到第一次调用该函数时才存在 包含它 此后它在人的一生中
  • 将 ItemsControl ItemSource 绑定到 UserControl 依赖属性

    这是我第一次尝试创建具有依赖属性的用户控件 所以请原谅我对这个主题缺乏了解 我在其中一个页面上创建了一个总体设计 我想将其转换为可重用的用户控件 页面上的原始控件 这就是我试图移植到可重用 UserControl 的控件