为了账号安全,请及时绑定邮箱和手机立即绑定

c#绑定前更改属性值

c#绑定前更改属性值

C#
子衿沉夜 2021-07-20 16:57:52
我不确定之前是否有人问过这个问题,但我目前处于将控件的属性绑定到 a 的情况。DependencyProperty但是,返回的值Property是类型Double。设置绑定时,如何20从属性中减去或给定值,然后绑定控件?我需要为此实施IValueConverter吗?我仍在学习 WPF,因此将不胜感激。依赖属性public static readonly DependencyProperty ProgressbarValueDependency = DependencyProperty.Register("PrValue", typeof(double),     typeof(LinearProgressBar));public double PrValue{    get    {        return System.Convert.ToDouble(GetValue(ProgressbarValueDependency));    }    set    {        SetValue(ProgressbarValueDependency, value);    }}绑定到属性 {MainGrid = GetTemplateChild("MainGrid");Binding MainGridWidthBinding = new Binding("PrValue"){    Source = this,    Mode = BindingMode.TwoWay};MainGrid.SetBinding(Grid.WidthProperty, MainGridWidthBinding);}
查看完整描述

2 回答

?
SMILET

TA贡献1796条经验 获得超4个赞

为了达到你的目的,你需要编写一个类实现IValueConverter接口。


public class MyConverter : IValueConverter

{

    public object Convert(object value, Type targetType, 

        object parameter, string language)

    {

        // Contain your source to target convertion logic. 

    }


    public object ConvertBack(object value, Type targetType, 

        object parameter, string language)

    {

        // Contain your target to source convertion logic.

        // Only needed if using TwoWay or OneWayToSource Binding Mode. 

    }

}

然后Binding.Converter在调用SetBinding方法之前将其实例设置为属性 。


Binding MainGridWidthBinding = new Binding("PrValue")

{

    Source = this,

    Mode = BindingMode.TwoWay,

    Converter = new MyConverter(),

};

MainGrid.SetBinding(Grid.WidthProperty, MainGridWidthBinding);


查看完整回答
反对 回复 2021-07-31
  • 2 回答
  • 0 关注
  • 168 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信