【问题标题】:Binding ProgressBar in CustomControl seems not to work在 CustomControl 中绑定 ProgressBar 似乎不起作用
【发布时间】:2016-01-15 10:02:37
【问题描述】:

我有一个自定义控件,其中包含一个 ProgressBar 以及其他元素。

<ProgressBar Value="{TemplateBinding CurrentProgress}"
             MinValue="{TemplateBinding MinValue}"
             MaxValue="{TemplateBinding MaxValue}"/>

<Label Content="{TemplateBinding CurrentProgress}"/>

在我的 .cs 文件中,我定义了所有这些属性,如下所示:

#region MaxProgress
public int MaxProgress
{
    get { return (int)GetValue(MaxProgressProperty); }
    set { SetValue(MaxProgressProperty, value); }
}

public static readonly DependencyProperty MaxProgressProperty =
        DependencyProperty.Register("MaxProgress", typeof(int), typeof(GameFlowControl), new FrameworkPropertyMetadata(1000, FrameworkPropertyMetadataOptions.AffectsRender)); 
#endregion

#region CurrentProgress
public int CurrentProgress
{
    get { return (int)GetValue(CurrentProgressProperty); }
    set { SetValue(CurrentProgressProperty, value); }
}

public static readonly DependencyProperty CurrentProgressProperty =
    DependencyProperty.Register("CurrentProgress", typeof(int), typeof(GameFlowControl), new FrameworkPropertyMetadata(50, FrameworkPropertyMetadataOptions.AffectsRender)); 
#endregion

#region MinProgress
public int MinProgress
{
    get { return (int)GetValue(MinProgressProperty); }
    set { SetValue(MinProgressProperty, value); }
}

public static readonly DependencyProperty MinProgressProperty =
    DependencyProperty.Register("MinProgress", typeof(int), typeof(GameFlowControl), new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.AffectsRender));
#endregion

如上所示将这些值绑定到标签上可以正常工作,但显然这些绑定不适用于我的 ProgressBar。到目前为止我所尝试的:

  • 更改 Value、MinValue 和 MaxValue 的顺序。
  • 在 TemplateBinding 中添加了一个错字(如 CurrentProgressXYZ),这给了我一个编译错误(因此可以识别属性)
  • 为属性添加了默认值(参见 0、50、1000)。
  • 直接删除了绑定和设置值:Value = 50, MinValue = 0, MaxValue=100,这表明 ProgressBar 显示为半填充。
  • 为这些属性的 getter 添加了断点,它们没有被触发(这让我很困惑!)

有什么可能导致这种情况的提示吗?

【问题讨论】:

  • 你误用了TemplateBinding
  • they were not triggered 绑定引擎不使用 CLR 属性,它直接使用依赖属性。
  • 你试过后台工作吗?

标签: c# wpf binding custom-controls templatebinding


【解决方案1】:

根据你的问题回答

在这种情况下你应该使用的绑定方法是Value="{Binding CurrentProgress, RelativeSource={RelativeSource AncestorType={x:Type GameFlowControl}}}"。这会向上遍历可视化树,找到第一个 GameFlowControl 控件,然后从该相对位置绑定到路径。

另一种选择

如果您没有将UserControl 中的DataContext 用于任何其他目的,则可以使用较短的绑定方法。

首先,您需要使用以下方式将DataContext 分配给派生的UserControl 引用:-

    public GasFlowControl()
    {
        InitializeComponent();

        DataContext = this;  //Set the DataContext to point to the control itself

    }

那么你的绑定可以简化为:-

    <ProgressBar Value="{Binding CurrentProgress}"
         MinValue="{Binding MinValue}"
         MaxValue="{Binding MaxValue}"/>

    <Label Content="{Binding CurrentProgress}"/>

回答你的困惑

为这些属性的 getter 添加了断点,它们没有被触发(这让我很困惑!)

您没有为属性 Getters 和 Setters 触发任何断点的原因是 WPF 框架不使用它们。它在内部直接调用GetValue(CurrentProgressProperty);SetValue(CurrentProgressProperty, value);。它们只是为了方便您将其包含在您的代码中,并方便进行类型转换,从而在编译时进行类型检查。

如果您的代码不使用它们,那么它们将永远不会被调用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 2017-12-02
    • 2010-11-02
    • 1970-01-01
    相关资源
    最近更新 更多