gpt4 book ai didi

c# - WPF 设置用户控件依赖属性

转载 作者:行者123 更新时间:2023-11-30 19:54:00 24 4
gpt4 key购买 nike

在我的用户控件下面:

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

最佳答案

您必须绑定(bind)到实际属性,而不是其标识符字段,即 ControlWidth 而不是 ControlWidthProperty

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

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

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

但是请注意,这些绑定(bind)都没有真正意义。当已经有一个 Width 时,添加一个 ControlWidth 属性是没有意义的。

在 Viewbox 中没有必要绑定(bind) 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>

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

关于c# - WPF 设置用户控件依赖属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44913478/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com