WPF 设置用户控件依赖属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44913478/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 14:07:23  来源:igfitidea点击:

WPF Set user control dependency properties

c#wpfxamldependency-properties

提问by Ralph

Below a piece of my user control:

下面是我的用户控件的一部分:

<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>

and its code-behind with my dependency properties:

以及它的代码隐藏在我的依赖属性中:

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

Then from my wpf main window:

然后从我的 wpf 主窗口:

    <ctr:CircularProgressBar x:Name="progressBar" Grid.ZIndex="3"                             
                         HorizontalAlignment="Center"
                         VerticalAlignment="Center"
                         ControlHeight="100"
                         ControlWidth="100"/>   

What I am trying to do is set width and height for my user control from my main window. In above example I am trying to set user control height and width to 100 through dependency properties ControlHeight and ControlWidth respectively.

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

If from my main windows ControlHeight and ControlWidth is not specified, I want user control height and width take as default value of 45.

如果从我的主窗口 ControlHeight 和 ControlWidth 未指定,我希望用户控件的高度和宽度作为默​​认值 45。

But above example is not working for me. What am I doing wrong?

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

UPDATE WORKING: As Clemens suggested, I have changed the code into the following:

更新工作:正如克莱门斯所建议的,我已将代码更改为以下内容:

<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>

In code-behind dependency properties ControlHeightProperty and ControlWidthProperty are not needed.

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

Finally in my wpf window, setting typical height and width properties are enough:

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

    <ctr:CircularProgressBar x:Name="progressBar" Grid.ZIndex="3"                             
                         HorizontalAlignment="Center"
                         VerticalAlignment="Center"
                         Height="100"
                         Width="100"/>   

回答by Clemens

You'll have to bind to the actual property, not its identifier field, i.e. ControlWidthinstead of ControlWidthProperty.

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

Besides that, you also have to set a binding source, which in this case is the UserControl instance, referenced either by RelativeSource Selfat the UserControl level, or RelativeSource AncestorType=UserControlat any level below.

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

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

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

Note however that none of these bindings really make sense. There is no point in adding a ControlWidthproperty when there already is a Width.

但是请注意,这些绑定都没有真正意义。有一个在加入一个没有点ControlWidth属性时,已经有一个Width

At the Viewbox it isn't necessary to bind the Width or Height because the UserControl will already resize it appropriately.

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

So in fact you don't need any additional property. Your UserControl's XAML should look like shown below, without explicitly setting anyWidth or Height.

所以实际上你不需要任何额外的属性。您的 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>

When you use that control somewhere in your MainWindow, instead of setting ControlWidth and ControlHeight, just set Width and Height.

当您在 MainWindow 的某处使用该控件时,无需设置 ControlWidth 和 ControlHeight,只需设置 Width 和 Height。