WPF设置用户控件依赖项属性

use*_*552 2 c# wpf xaml dependency-properties

在我的用户控件下面:

<UserControl x:Class="WpfApplication1.Controls.CircularProgressBar"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             Background="Transparent"
             Height="{Binding ControlHeightProperty}"
             Width="{Binding ControlWidthProperty}">

  <UserControl.Resources>
    <SolidColorBrush x:Key="progressCirclesColor" Color="#FF2E6187" />
  </UserControl.Resources>

    <Viewbox Width="{Binding ControlWidthProperty}" Height="{Binding ControlHeightProperty}" HorizontalAlignment="Center" VerticalAlignment="Center">
    <!-- other objects -->
    </Viewbox>
</UserControl>
Run Code Online (Sandbox Code Playgroud)

和我的依赖属性的代码隐藏:

public partial class CircularProgressBar
{
    public static readonly DependencyProperty ControlHeightProperty =
        DependencyProperty.Register("ControlHeight", typeof(int), typeof(CircularProgressBar), new UIPropertyMetadata(45));

    public static readonly DependencyProperty ControlWidthProperty =
        DependencyProperty.Register("ControlWidth", typeof(int), typeof(CircularProgressBar), new UIPropertyMetadata(45));

    public int ControlHeight
    {
        get { return (int)GetValue(ControlHeightProperty); }
        set { SetValue(ControlHeightProperty, value); }
    }

    public int ControlWidth
    {
        get { return (int)GetValue(ControlWidthProperty); }
        set { SetValue(ControlWidthProperty, value); }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后从我的wpf主窗口:

    <ctr:CircularProgressBar x:Name="progressBar" Grid.ZIndex="3"                             
                         HorizontalAlignment="Center"
                         VerticalAlignment="Center"
                         ControlHeight="100"
                         ControlWidth="100"/>   
Run Code Online (Sandbox Code Playgroud)

我想要做的是从我的主窗口设置我的用户控件的宽度和高度.在上面的例子中,我试图通过依赖属性ControlHeight和ControlWidth分别将用户控件高度和宽度设置为100.

如果未指定我的主窗口ControlHeight和ControlWidth,我希望用户控件的高度和宽度取默认值45.

但上面的例子对我不起作用.我究竟做错了什么?

更新工作:正如Clemens建议的那样,我已将代码更改为以下内容:

<UserControl x:Class="WpfApplication1.Controls.CircularProgressBar"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             Background="Transparent">

  <UserControl.Resources>
    <SolidColorBrush x:Key="progressCirclesColor" Color="#FF2E6187" />
  </UserControl.Resources>

    <Viewbox HorizontalAlignment="Center" VerticalAlignment="Center">
    <!-- other objects -->
    </Viewbox>
</UserControl>
Run Code Online (Sandbox Code Playgroud)

在代码隐藏依赖项属性中,不需要ControlHeightProperty和ControlWidthProperty.

最后在我的wpf窗口中,设置典型的高度和宽度属性就足够了:

    <ctr:CircularProgressBar x:Name="progressBar" Grid.ZIndex="3"                             
                         HorizontalAlignment="Center"
                         VerticalAlignment="Center"
                         Height="100"
                         Width="100"/>   
Run Code Online (Sandbox Code Playgroud)

Cle*_*ens 7

你必须绑定到实际的属性,而不是它的标识符字段,即ControlWidth代替ControlWidthProperty.

除此之外,您还必须设置一个绑定源,在本例中是UserControl实例,RelativeSource Self在UserControl级别或RelativeSource AncestorType=UserControl下面的任何级别引用.

<UserControl Width="{Binding ControlWidth, RelativeSource={RelativeSource Self}}" ...>

<Viewbox Width="{Binding ControlWidth,
                 RelativeSource={RelativeSource AncestorType=UserControl}}" ...>
Run Code Online (Sandbox Code Playgroud)

但请注意,这些绑定都不是真的有意义.在已经存在的情况下添加ControlWidth属性是没有意义的Width.

在Viewbox中,没有必要绑定Width或Height,因为UserControl已经适当地调整了它的大小.

所以实际上你不需要任何额外的财产.您的UserControl的XAML应如下所示,而不显式设置任何宽度或高度.

<UserControl x:Class="WpfApplication1.Controls.CircularProgressBar"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             Background="Transparent">
    <UserControl.Resources>
        <SolidColorBrush x:Key="progressCirclesColor" Color="#FF2E6187" />
    </UserControl.Resources>
    <Viewbox>
        <!-- other objects -->
    </Viewbox>
</UserControl>
Run Code Online (Sandbox Code Playgroud)

当您在MainWindow中的某个位置使用该控件时,只需设置Width和Height,而不是设置ControlWidth和ControlHeight.