WPF用户控件:绑定到属性的属性

时间:2011-07-26 03:11:00

标签: c# wpf xaml data-binding user-controls

我正在尝试创建一个WPF用户控件,基本上是一个文本块,每边都有小圆圈来显示短数值。我创建了一个类(LabelProperties),它封装了每个圆的设置(文本,前景画笔,背景画笔,可见性),然后为左侧和右侧圆的用户控件添加了一个属性。控件通常按照我的意愿工作,但数据绑定不适用于圆形文本。

我是WPF的新手,我猜我试图以绑定模型不支持的方式完成此任务。我非常感谢任何帮助。

LabelProperties类

public class LabelProperties : DependencyObject
{
    public static readonly DependencyProperty TextProperty = 
        DependencyProperty.Register("Text", typeof(string), typeof(LabelProperties));
    public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }

    public static readonly DependencyProperty VisibilityProperty = 
        DependencyProperty.Register("Visibility", typeof(Visibility), typeof(LabelProperties));
    public Visibility Visibility
    {
        get { return (Visibility)GetValue(VisibilityProperty); }
        set { SetValue(VisibilityProperty, value); }
    }

    public static readonly DependencyProperty ForegroundProperty = 
        DependencyProperty.Register("Foreground", typeof(Brush), typeof(LabelProperties));
    public Brush Foreground
    {
        get { return (Brush)GetValue(ForegroundProperty); }
        set { SetValue(ForegroundProperty, value); }
    }

    public static readonly DependencyProperty BackgroundProperty = 
        DependencyProperty.Register("Background", typeof(Brush), typeof(LabelProperties));
    public Brush Background
    {
        get { return (Brush)GetValue(BackgroundProperty); }
        set { SetValue(BackgroundProperty, value); }
    }

用户控制标记

<UserControl x:Class="ControlTest.DisplayBlock"
             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" 
             mc:Ignorable="d" 
             d:DesignHeight="106" d:DesignWidth="251"
             x:Name="_userControl">
    <Grid x:Name="_grid" Height="40" >
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="30" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="30" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="26" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Border Background="{Binding BlockBackground, ElementName=_userControl, Mode=TwoWay}" 
                Grid.ColumnSpan="3" Grid.RowSpan="2" CornerRadius="10" BorderThickness="1" BorderBrush="Black"  Margin="12,0">
        </Border>
        <Ellipse Fill="{Binding Path=LeftCircle.Background, ElementName=_userControl, Mode=TwoWay}" 
                 Visibility="{Binding Path=LeftCircle.Visibility, ElementName=_userControl, Mode=TwoWay}"
                 Stroke="Black" Grid.Column="0" StrokeThickness="2" Margin="2" />
        <TextBlock Foreground="{Binding Path=LeftCircle.Foreground, ElementName=_userControl, Mode=TwoWay}" 
                   Text="{Binding Path=LeftCircle.Text, ElementName=_userControl, Mode=TwoWay}"
                   HorizontalAlignment="Center" VerticalAlignment="Center" FontWeight="Bold" Grid.Column="0" />
        <Ellipse Fill="{Binding Path=RightCircle.Background, ElementName=_userControl, Mode=TwoWay}" 
                 Visibility="{Binding Path=RightCircle.Visibility, ElementName=_userControl, Mode=TwoWay}"
                 Stroke="Black" Grid.Column="2" StrokeThickness="2" Margin="2" />
        <TextBlock Foreground="{Binding Path=RightCircle.Foreground, ElementName=_userControl, Mode=TwoWay}" 
                   Text="{Binding Path=RightCircle.Text, ElementName=_userControl, Mode=TwoWay}"
                   HorizontalAlignment="Center" VerticalAlignment="Center" FontWeight="Bold" Grid.Column="2" />
        <TextBlock Text="{Binding Path=Text, ElementName=_userControl, Mode=TwoWay}" 
                   Grid.Column="1" Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.RowSpan="2" FontSize="16" Margin="4" />
    </Grid>
</UserControl>

用户控制代码隐藏

public partial class DisplayBlock : UserControl
{
    public DisplayBlock()
    {
        InitializeComponent();
    }

    public static readonly DependencyProperty TextProperty = 
        DependencyProperty.Register("Text", typeof(string), typeof(DisplayBlock));
    public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }

    public static readonly DependencyProperty BlockBackgroundProperty =
        DependencyProperty.Register("BlockBackground", typeof(Brush), typeof(DisplayBlock));
    public Brush BlockBackground
    {
        get { return (Brush)GetValue(BlockBackgroundProperty); }
        set { SetValue(BlockBackgroundProperty, value); }
    }

    public static readonly DependencyProperty LeftCircleProperty =
        DependencyProperty.Register("LeftCircle", typeof(LabelProperties), typeof(DisplayBlock));
    public LabelProperties LeftCircle
    {
        get { return (LabelProperties)GetValue(LeftCircleProperty); }
        set { SetValue(LeftCircleProperty, value); }
    }

    public static readonly DependencyProperty RightCircleProperty =
        DependencyProperty.Register("RightCircle", typeof(LabelProperties), typeof(DisplayBlock));
    public LabelProperties RightCircle
    {
        get { return (LabelProperties)GetValue(RightCircleProperty); }
        set { SetValue(RightCircleProperty, value); }
    }

0 个答案:

没有答案