简体   繁体   English

WPF和C#中路径格式化的绑定元素

[英]Binding Element at Path formatting in WPF and C#

I'm user a custom user control that is a special type of slider. 我正在使用一种特殊类型的滑块的自定义用户控件。 I can get the lower and upper values from the binding of the object like such: 我可以从对象的绑定中获得较低和较高的值,如下所示:

{Binding ElementName=slider, Path=LowerValue};
{Binding ElementName=slider, Path=UpperValue};

I would like to reformat this number before putting into a Label . 在放入Label之前,我想重新格式化此数字。 I've tried embedding some c# directly into my WPF, but I'm not sure how to get the values back in c#. 我尝试将一些c#直接嵌入到我的WPF中,但是我不确定如何在c#中获取值。 Here's what I've tried which doesn't work: 这是我尝试过的不起作用的方法:

<![CDATA[
                double min = {Binding ElementName=slider, Path=LowerValue};
                double max = {Binding ElementName=slider, Path=UpperValue};


                if (min == 1) { label.Content = "24:00:00"; }
                else { label.Content = TimeSpan.FromHours(min).ToString(@"hh\:mm\:ss"); }

                if (max == 1) { label_Copy.Content = "24:00:00"; }
                else { label_Copy.Content = TimeSpan.FromHours(max).ToString(@"hh\:mm\:ss"); }
            ]]>
    </x:Code>

This sort of thing is a lot easier if you use MVVM. 如果使用MVVM,这种事情要容易得多。 Create a view model and add your two properties along with an update function: 创建一个视图模型,并添加两个属性以及一个更新函数:

public class TestViewModel : ViewModelBase
{
    private int _TimeVal;
    public int TimeVal
    {
        get { return this._TimeVal; }
        set
        {
            this._TimeVal = value;
            RaisePropertyChanged(() => this.TimeVal);
            UpdateText();
        }
    }

    private string _TimeText;
    public string TimeText
    {
        get { return this._TimeText; }
        set { this._TimeText = value; RaisePropertyChanged(() => this.TimeText); }
    }

    public TestViewModel()
    {
        UpdateText(); // force initial value
    }

    private void UpdateText()
    {
        this.TimeText = TimeSpan.FromHours(this.TimeVal).ToString(@"hh\:mm\:ss");
    }
}

Then in your XAML you simply bind to those properties: 然后,在XAML中,您只需绑定到这些属性:

<Slider Minimum="0" Maximum="100" Value="{Binding TimeVal}"/>
<TextBox Text="{Binding Path=TimeText}" />

1.Define a converter that casts the value as slider & returns the formatted text: 1.定义一个转换器,将值转换为滑块并返回格式化的文本:

public class SliderToContentConverter:IValueConverter
{
  ....Convert(object value,...)
  {
     var slider =value as slider;
     if(slider!=null)
     {
      double min = slider.LowerValue;
      double max = slider.UpperValue;
      if (min == 1) 
         { return "24:00:00"; }
      else
         { return TimeSpan.FromHours(min).ToString(@"hh\:mm\:ss"); }
      if (max == 1) 
         { return "24:00:00"; }
      else {  return TimeSpan.FromHours(max).ToString(@"hh\:mm\:ss"); 
      }
     return null;
   }
}

2.Add the converter in Resources with a key. 2.使用密钥在资源中添加转换器。
3.Add binding for Label.Content: 3.为Label.Content添加绑定:

<Label x:name=label_copy Content={Binding ElementName=slider,Converter={StaticResource con}/>
<slider LowerValue={Binding lval} UpperValue={Binding uval}/>

in c# 在C#中

double _lval;
double lval
{
get
{
return _lval;
}
set
{
_lval=value;
}
}

value is what you want 价值就是你想要的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM