WPF用户控制属性

本文关键字:属性 控制 用户 WPF | 更新日期: 2023-09-27 18:08:22

在下面的代码中,我有一个包含一个椭圆和一个文本块的UserControl。我想创建一个可重用的控件,我可以绑定到它,允许我设置基于字符串的文本,并根据布尔值改变红色/绿色之间的填充颜色。

我现在可以通过深入挖掘标记和使用一些复杂的绑定来做到这一点,但是我想在列表中重用这个控件,为此目的创建一个控件似乎更容易。然而,我不确定下一步要去哪里,如果我应该创建与FillText的值相关联的依赖属性,或者什么。

<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    x:Class="Herp.Derp.View.DeliveryStatusIndicator"
    x:Name="UserControl"
    d:DesignWidth="91" d:DesignHeight="35">
    <Grid x:Name="LayoutRoot">
        <StackPanel  Orientation="Horizontal">
            <Ellipse Width="35" Height="35" Fill="Green">
                <Ellipse.OpacityMask>
                    <VisualBrush Stretch="Fill" Visual="{StaticResource appbar_location_circle}"/>
                </Ellipse.OpacityMask>
            </Ellipse>
            <TextBlock Style="{StaticResource Heading2}" 
                       VerticalAlignment="Center" Margin="3,0,0,0">
                 <Run Text="FRONT"/>
            </TextBlock>
        </StackPanel>
    </Grid>
</UserControl>

WPF用户控制属性

如何做到这一点

用户控件

/// <summary>
/// Interaction logic for UserControl1.xaml
/// </summary>
public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();
    }
       public static readonly DependencyProperty FrontTextProperty = DependencyProperty.Register( "FrontText", typeof(string),typeof(UserControl1), new FrameworkPropertyMetadata(string.Empty));
    public string FrontText
    {
        get { return (string)GetValue(FrontTextProperty); }
        set {
            SetValue(FrontTextProperty, value);
            frontBlock.Text = value;  
        }
    }
    public static readonly DependencyProperty EllipseStateProperty = DependencyProperty.Register("EllipseState", typeof(bool), typeof(UserControl1), new FrameworkPropertyMetadata(false));
    public bool EllipseState
    {
        get { return (bool)GetValue(EllipseStateProperty); }
        set
        {
            SetValue(EllipseStateProperty, value);
            if (value)
            {
                ellipse.Fill  = new SolidColorBrush( Colors.Green);  
            }
            else
            {
                ellipse.Fill = new SolidColorBrush(Colors.Red);
            }
        }
    }

}

主窗口

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1" x:Class="WpfApplication1.MainWindow"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <local:UserControl1 EllipseState="{Binding yourProperty }"/>
        <CheckBox Content="CheckBox" HorizontalAlignment="Left" Margin="207,94,0,0" VerticalAlignment="Top"/>
    </Grid>
</Window>

是的,要创建"父"XAML可以分配绑定的属性,您需要为您想要绑定的每个字段创建DependencyProperty

然后将用户控件xaml绑定到DP的backing属性。

这是我得到的一个可行的解决方案:

public partial class DeliveryStatusIndicator : UserControl
{
    public DeliveryStatusIndicator()
    {
            InitializeComponent();          
    }
    public static readonly DependencyProperty DeliveryDescriptionProperty = DependencyProperty.Register("DeliveryDescription", typeof(string), typeof(DeliveryStatusIndicator), new FrameworkPropertyMetadata("Default", DescriptionChangedEventHandler));
     private static void DescriptionChangedEventHandler(DependencyObject d, DependencyPropertyChangedEventArgs e)
     {
         ((DeliveryStatusIndicator)d).Desc.Text = (string)e.NewValue;
     }
     public string DeliveryDescription
     {
         get { return (string)GetValue(DeliveryDescriptionProperty); }
         set
         {
             SetValue(DeliveryDescriptionProperty, value);
         }
     }
     public static readonly DependencyProperty DeliveryStatusProperty = DependencyProperty.Register("DeliveryStatus", typeof(bool), typeof(DeliveryStatusIndicator), new FrameworkPropertyMetadata(false, StatusChangedEventHandler));
     private static void StatusChangedEventHandler(DependencyObject d, DependencyPropertyChangedEventArgs e)
     {
         ((DeliveryStatusIndicator)d).Indicator.Fill = (bool)e.NewValue ? new SolidColorBrush(Colors.Green) : new SolidColorBrush(Colors.Red);
     }
     public bool DeliveryStatus
     {
         get { return (bool)GetValue(DeliveryStatusProperty); }
         set
         {
             SetValue(DeliveryStatusProperty, value);
         }
     }

}