[英]Multi value converter does not update value
我有MultiValueConverter,它可以精确显示值。 但似乎它不会更新UI。 如何解决这个问题? 我不能在xaml中使用字符串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;
}
}
您要查看转换后的值吗?
更换:
<ToggleButton.CommandParameter>
用
<ToggleButton.Content>
如果不能在XAML中使用字符串格式化程序,则必须创建一个随时可用的属性以绑定到模型中。
public string BindMe
{
get { return string.Format("{0} : {1}", Rate, Precision); }
}
然后在“ Rate
和Precision
的设置器中,
RaisePropertyChanged("BindMe");
因为这些将暗示对BindMe
的更新。
并在XAML中对BindMe
进行简单绑定。
<ToggleButton.Content>
<TextBlock Text="{Binding BindMe}" />
</ToggleButton.Content>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.