相关疑难解决方法(0)

如何:覆盖依赖项属性的元数据

如何覆盖默认依赖项属性元数据.例如; textbox的Text属性.我用这个代码

           class UCTextBox : TextBox
       {
           public UCTextBox()
        {
       var defaultMetadata = TextBox.TextProperty.GetMetadata(typeof(TextBox));

       TextBox.TextProperty.OverrideMetadata(typeof(UCTextBox),
     new          FrameworkPropertyMetadata(string.Empty,
        FrameworkPropertyMetadataOptions.Journal | 
     FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
        defaultMetadata.PropertyChangedCallback,
        new CoerceValueCallback(CoerceText)
        )); 
}

    private static object CoerceText(DependencyObject d, object value)
     {
     return   value.ToString().Replace(",","");           
    }
Run Code Online (Sandbox Code Playgroud)

但这在两次运行中(获取,设置)

没有人可以帮助我!!!:(((

wpf

5
推荐指数
1
解决办法
8298
查看次数

依赖属性继承

我需要我的控件从Grid类型的祖先那里继承UIElement.IsEnabledProperty(可以选择Window或其他任何我可以用来包装我的网格的元素)

CS:在下面,我重写UIElement.IsEnabledProperty的元数据,并使用Change和Coerce委托对其进行设置。

    static PipeControl()
    {
        PipeControl.IsEnabledProperty.OverrideMetadata(typeof(PipeControl), new FrameworkPropertyMetadata(false, OnIsEnabledPropertyChanged, OnIsEnabledPropertyCoerce));
    }

    private static void OnIsEnabledPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var isEnabled = (bool)e.NewValue;
    }

    private static object OnIsEnabledPropertyCoerce(DependencyObject d, object baseValue)
    {
        var valueSource = DependencyPropertyHelper.GetValueSource(d, PipeControl.IsEnabledProperty);

        var pipeContorl = d as PipeControl;
        if (pipeContorl == null) return baseValue;

        return (bool)baseValue && pipeContorl.IsMyPipe;
    }
Run Code Online (Sandbox Code Playgroud)

XAML:

    <Grid IsEnabled="{Binding IsMyCondition , Mode=OneWay}">        
         <game:PipeControl Grid.Row="2"  />            
         <game:PipeControl  Grid.Row="2" Grid.Column="1" />
    </Grid> 
Run Code Online (Sandbox Code Playgroud)

每次IsMyCondition更改时,每个PipeContorl中都会调用一次OnIsEnabledPropertyCoerce,从不调用OnIsEnabledPropertyChanged,OnIsEnabledProerty Coerce中的ValueSource为“ Default”(显示Coerce始终获取默认的假值)。

我必须以需要使用继承的方式错过某些东西,我希望值源被“继承”,并调用OnIsEnabledPropertyChanged。

wpf inheritance dependency-properties custom-controls

2
推荐指数
1
解决办法
3508
查看次数