多值转换器不会更新值

时间:2015-10-26 14:45:15

标签: c# wpf data-binding

我有MultiValueConverter,它精确显示值。但似乎它没有更新UI。怎么可能解决这个问题?我不能在xaml中使用string fromat,因为可以在Update()中更改精度。它只是在Update()函数中指定转换器的精度的一种方法吗?

的Xaml:

 <Window.Resources>
    <design:PrecisionConverter x:Key="PrecisionConverter"/>
</Window.Resources>
<Grid>
    <ToggleButton Height="30" Width="90" >
        <ToggleButton.CommandParameter>
            <MultiBinding Converter="{StaticResource PrecisionConverter}">
                <Binding Path="Rate"/>
                <Binding Path="Precision"/>
            </MultiBinding>
        </ToggleButton.CommandParameter>
    </ToggleButton>
</Grid>

转换器:

class PrecisionConverter:IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        int precision = int.Parse(values[1].ToString());
        double Value = double.Parse(values[0].ToString());
        NumberFormatInfo nfi = new NumberFormatInfo();
        nfi.NumberDecimalDigits = precision;
        return Value.ToString("N",nfi);
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

主:

namespace WpfApplication186
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{

    public MainWindow()
    {

        InitializeComponent();
        DataContext = new Data();
    }
}
public class Data:INotifyPropertyChanged
{
    private double rate;
    public double Rate 
    { 
        get
        {
            return this.rate;
        }

        set
        {
            this.rate = value;
            this.RaisePropertyChanged("Rate");
        }
    }

    private int precision;
    public int Precision
    {
        get
        {
            return this.precision;
        }

        set
        {
            this.precision = value;
            this.RaisePropertyChanged("Precision");
        }
    }
    public Data()
    {
        Action Test = new Action(Update);
        IAsyncResult result = Test.BeginInvoke(null,null);
    }
    public void Update()
    {
        while(true)
        {
            System.Threading.Thread.Sleep(1000);
            Rate += 0.4324232;
            Precision = 2;
        }
    }

    private void RaisePropertyChanged(string info)
    {
        if (PropertyChanged!=null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

}

2 个答案:

答案 0 :(得分:1)

您想要查看转换后的值吗?

替换:

<ToggleButton.CommandParameter>

<ToggleButton.Content>

答案 1 :(得分:-1)

如果您不能在XAML中使用字符串格式化程序,则必须在模型中创建要绑定的随时可用属性。

public string BindMe 
{
  get { return string.Format("{0} : {1}", Rate, Precision); }
}

RatePrecision的制定者中,请致电

RaisePropertyChanged("BindMe");

因为那些暗示更新BindMe

在XAML中执行与BindMe的简单绑定。

<ToggleButton.Content>
  <TextBlock Text="{Binding BindMe}" />
</ToggleButton.Content>