col*_*red 9 c# wpf xaml binding
我有一个应用程序,它使用两个滑块生成代码中其他地方使用的产品.我想要的是将产品值绑定到文本块或工具提示,例如,看起来像"10 x 15 = 150".
第一部分很简单,看起来像这样:
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} x {1}">
<Binding ElementName="amount_slider" Path="Value" />
<Binding ElementName="frequency_slider" Path="Value"/>
</MultiBinding>
</TextBlock.Text>
Run Code Online (Sandbox Code Playgroud)
但是,将产品放在那里的简单方法是什么?
使用Pavlo Glazkov的解决方案,我将其修改为如下所示:
public class MultiplyFormulaStringConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
var doubleValues = values.Cast<double>().ToArray();
double x = doubleValues[0];
double y = doubleValues[1];
var leftPart = x.ToString() + " x " + y.ToString();
var rightPart = (x * y).ToString();
var result = string.Format("{0} = {1}", leftPart, rightPart);
return result;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Run Code Online (Sandbox Code Playgroud)
而且非常重要
<Window.Resources>
<local:MultiplyFormulaStringConverter x:Key="MultiplyFormulaStringConverter"/>
</Window.Resources>
Run Code Online (Sandbox Code Playgroud)
谢谢!
Pav*_*kov 14
而不是使用StringFormat创建转换器.像这样的东西:
public class MultiplyFormulaStringConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
var doubleValues = values.Cast<double>().ToArray();
var leftPart = string.Join(" x ", doubleValues);
var rightPart = doubleValues.Sum().ToString();
var result = string.Format("{0} = {1}", leftPart, rightPart);
return result;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Run Code Online (Sandbox Code Playgroud)
<TextBlock.Text>
<MultiBinding Converter="{StaticResource MultiplyFormulaStringConverter}">
<Binding ElementName="amount_slider" Path="Value" />
<Binding ElementName="frequency_slider" Path="Value"/>
</MultiBinding>
</TextBlock.Text>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10182 次 |
| 最近记录: |