I have MultiValueConverter which show value with precision. But it seems that it does not update UI. How it is possible to solve this problem? I can not use string fromat in xaml because precision can be changed in Update()
. Is it only one way to specify precision in Update()
function witout converter?
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>
Converter:
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();
}
}
Main:
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;
}
}
You want to see the converted value?
Replace:
<ToggleButton.CommandParameter>
With
<ToggleButton.Content>
If you can't use a string formatter in XAML, you will have to create a ready-to-go property to bind to in your model.
public string BindMe
{
get { return string.Format("{0} : {1}", Rate, Precision); }
}
And in the setters for Rate
and Precision
, call
RaisePropertyChanged("BindMe");
Since those will imply an update to BindMe
.
And in XAML do a simple bind to BindMe
.
<ToggleButton.Content>
<TextBlock Text="{Binding BindMe}" />
</ToggleButton.Content>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.