WPF设置用户控件依赖属性

3
下面是我的用户控件代码片段:

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

我使用依赖属性编写了与其代码文件相对应的逻辑代码:

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

然后从我的WPF主窗口开始:
    <ctr:CircularProgressBar x:Name="progressBar" Grid.ZIndex="3"                             
                         HorizontalAlignment="Center"
                         VerticalAlignment="Center"
                         ControlHeight="100"
                         ControlWidth="100"/>   

我想做的是从我的主窗口中设置我的用户控件的宽度和高度。在上面的示例中,我尝试通过依赖属性 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>

在后台代码中,不需要使用ControlHeightProperty 和 ControlWidthProperty 依赖属性。最终在我的WPF窗口中,设置典型的高度和宽度属性就足够了:
    <ctr:CircularProgressBar x:Name="progressBar" Grid.ZIndex="3"                             
                         HorizontalAlignment="Center"
                         VerticalAlignment="Center"
                         Height="100"
                         Width="100"/>   


你是否遇到绑定错误? - NtFreX
除了问题本身,你知道这些属性没有意义,因为已经有宽度和高度了? - Clemens
@Clemens 我认为他只想在用户控件中定义控件的大小。 用户控件的大小可以是任何其他大小。 - NtFreX
@NtFreX,你有没有看到UserControl本身的Width和Height绑定?Viewbox将填充整个UserControl区域,因此那里的绑定是无意义的。 - Clemens
@Clemens 不,我没有这样做,可能会添加。但你是对的。如果他想设置用户控件的大小,这不是正确的方法。 - NtFreX
@NtFreX 我的目的是在主窗口中设置用户控件的大小,而用户控件是被导入和使用的。 - Willy
1个回答

9

您需要绑定实际属性,而不是其标识符字段,即使用ControlWidth而不是ControlWidthProperty

除此之外,您还需要设置绑定源,这种情况下绑定源为UserControl实例,可以在UserControl级别引用RelativeSource Self,或在任何下级级别引用RelativeSource AncestorType=UserControl

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

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

请注意,这些绑定都是没有意义的。如果已经有了Width属性,那么添加ControlWidth属性就毫无意义。

在视图框中,不需要绑定宽度或高度,因为用户控件会自动调整大小。

实际上,您不需要任何其他属性。您的用户控件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>

当您在MainWindow中的某个地方使用该控件时,不要设置ControlWidth和ControlHeight,只需设置Width和Height即可。


我的控制台短暂显示了“RelativeSource未处于FindAncestor模式”错误,但在我启动应用程序后它就消失了。很奇怪。 - NtFreX
你没有给我点踩吧?我做错了什么吗? - NtFreX
你看到我在问题上的评论了吗?我没有在你之前2分钟回答的唯一原因是我不想以“可能”开头回答。 - NtFreX
但无论如何,我已经为你的答案和问题点赞了,因为它们很好,这就是这个网站的做法。感谢你的时间。 - NtFreX
@Clemens,你的解决方案完美地运行了!谢谢。有一个问题; 将依赖属性设置为只读的含义是什么?我已经指定了它们并将其删除,但效果是相同的。 - Willy
只有标识符字段是只读的,而不是属性。该字段是静态和只读的,即在类级别上定义一次,无法被覆盖。 - Clemens

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接