【问题标题】:WPF Set user control dependency propertiesWPF 设置用户控件依赖属性
【发布时间】:2017-07-04 20:14:26
【问题描述】:

在我的用户控件下面:

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

【问题讨论】:

  • 您是否遇到绑定错误?
  • 除了问题中的实际问题,你知道这些属性没有意义,因为已经有了Width和Height?
  • @Clemens 我想他只想在用户控件中定义控件的大小。用户控件的大小可以是其他任何值。
  • @NtFreX 你看到 UserControl 本身的 Width 和 Height 绑定了吗?然后 Viewbox 将填充 UserControl 的整个区域,因此那里的绑定毫无意义。
  • @Clemens 不,我没有,它可能会被添加。但你是对的。如果他想设置用户控件的大小,这不是这样做的。

标签: c# wpf xaml dependency-properties


【解决方案1】:

您必须绑定到实际属性,而不是其标识符字段,即 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 属性是没有意义的。

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

所以实际上您不需要任何额外的属性。您的 UserControl 的 XAML 应如下所示,无需显式设置 any 宽度或高度。

<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 is not in FindAncestor mode 错误,但在我启动应用程序后它消失了。奇怪。
  • 我不怀疑你对我投了反对票?我是不是做错了什么?
  • 你看到我对这个问题的评论了吗?我没有在你之前 2 分钟回答的唯一原因是我不想以“可能”开头。
  • 但无论如何。我赞成你的回答和问题,因为它们很好,这就是它在这个网站上的完成方式。感谢您的宝贵时间。
  • @Clemens,您的解决方案完美运行!谢谢。一样东西;将依赖属性设置为只读是什么意思?我已经指定了,也去掉了,效果是一样的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-26
  • 1970-01-01
相关资源
最近更新 更多