【问题标题】:Binding CornerRadius to Dependency Property将 CornerRadius 绑定到依赖属性
【发布时间】:2014-01-29 18:34:15
【问题描述】:

我想绑定 CornerRadius 值。

CornerRadius="{Binding CornerRadiusInfoBtn}" 

这是TitleBar中的代码。

这是我的财产。

        public CornerRadius CornerRadiusInfoBtn
    {
        get { return (CornerRadius)GetValue(CornerRadiusInfoBtnProperty); }
        set { SetValue(CornerRadiusInfoBtnProperty, value); }
    }

    // Using a DependencyProperty as the backing store for CornerRadiusInfoBtn.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty CornerRadiusInfoBtnProperty =
        DependencyProperty.Register("CornerRadiusInfoBtn", typeof(CornerRadius), typeof(TitleBar), new PropertyMetadata(new CornerRadius(0.0,0.0,2.0,2.0)));

但是没有圆角。怎么了?

【问题讨论】:

  • 圆角在哪里?它在 CornerRadius 类中吗?
  • 您是否将数据上下文设置为TitleBar 的实例?
  • 当我将值设置为大于 0 时,角会变圆。
  • 属性在类MainWindowTitleBar?
  • 对不起,我的错误,属性在标题栏中

标签: c# wpf xaml binding cornerradius


【解决方案1】:

如果您在TitleBar.xaml.cs 文件中声明了您的DependencyProperty,那么您的TitleBar.xaml 文件应该包含一个Border,声明如下:

<Border CornerRadius="{Binding CornerRadiusInfoBtn, RelativeSource={RelativeSource  
    AncestorType={x:Type YourXmlNamespacePrefix:TitleBar}}, Mode=OneWay}}" ... />

您要么需要将DataContext 属性设置为自身(除非快速演示,否则不建议这样做),或者使用上述RelativeSource Binding 来定位该属性。

【讨论】:

    猜你喜欢
    • 2016-01-22
    • 2013-03-30
    • 2011-08-26
    • 2023-03-26
    • 2018-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多