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

C# WPF设置用户控件依赖项属性,c#,wpf,xaml,dependency-properties,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" Backgrou

下面是我的用户控件:

<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>
然后从我的wpf主窗口:

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


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

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

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

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

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



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

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

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

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

当您在主窗口中的某个位置使用该控件时,不要设置ControlWidth和ControlHeight,只需设置宽度和高度。

是否会出现绑定错误?除了问题中的实际问题,您知道这些属性没有意义,因为已经有宽度和高度了?@Clemens我想他只想定义usercontrol中控件的大小。usercontrol的大小可以是任何其他大小。@NtFreX您看到usercontrol本身的宽度和高度绑定了吗?然后,Viewbox将填充UserControl的整个区域,因此那里的绑定是毫无意义的。@Clemens不,我不知道,有可能会添加它。但你是对的。如果他想设置usercontrol的大小,这不是方法。我的控制台显示
RelativeSource在短时间内没有处于FindAncestor模式
错误,但在我启动应用程序后它就消失了。奇怪,而且我不怀疑你会贬低我?我做错什么了吗?你看到我对这个问题的回答了吗。我没有在你之前2分钟回答你的唯一原因是我不想以“可能”开头,但无论如何。我对你的答案和问题投了更高的票,因为它们都很好,这就是它在这个网站上的表现。谢谢您的时间。@Clemens,您的解决方案非常有效!谢谢。一件事;将依赖项属性设置为ReadOnly?的含义是什么?。我指定了它们,也删除了它们,效果是一样的。
<UserControl Width="{Binding ControlWidth, RelativeSource={RelativeSource Self}}" ...>

<Viewbox Width="{Binding ControlWidth,
                 RelativeSource={RelativeSource AncestorType=UserControl}}" ...>
<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>