这是我第一次尝试创建具有依赖属性的用户控件。所以请原谅我对这个主题缺乏了解。我在其中一个页面上创建了一个总体设计,我想将其转换为可重用的用户控件。
页面上的原始控件
这就是我试图移植到可重用 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>
这是该控件的外观图片
这是我的依赖属性代码
在这里你可以看到我创建了一个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 项源绑定到依赖项属性。我不知道这是我的语法还是我做错了整件事。如果有人能帮助指导我,那就太好了。
谢谢你,