C# 更改xaml中的绑定值
我想知道是否有一种方法可以将一个元素的属性绑定到另一个元素的属性,但可以修改中间的数据。例如,我可以将textblock的FontSize绑定到窗口的宽度/20或类似的值吗?现在我已经遇到过几次这样做很有用的地方,但总能找到解决办法(通常包括向我的viewModel中添加字段)。首选完全xaml解决方案。是,通过实施 对于转换器,您的场景将如下所示:C# 更改xaml中的绑定值,c#,wpf,data-binding,C#,Wpf,Data Binding,我想知道是否有一种方法可以将一个元素的属性绑定到另一个元素的属性,但可以修改中间的数据。例如,我可以将textblock的FontSize绑定到窗口的宽度/20或类似的值吗?现在我已经遇到过几次这样做很有用的地方,但总能找到解决办法(通常包括向我的viewModel中添加字段)。首选完全xaml解决方案。是,通过实施 对于转换器,您的场景将如下所示: [ValueConversion(typeof(double), typeof(double))] public class DivideBy20
[ValueConversion(typeof(double), typeof(double))]
public class DivideBy20Converter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var f = (double) value;
return f/20.0;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
var f = (double)value;
return f * 20.0;
}
}
。。。在XAML中是这样的:
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wpfApplication3="clr-namespace:WpfApplication3"
Title="MainWindow" Height="350" Width="525"
x:Name="Window">
<Window.Resources>
<wpfApplication3:DivideBy20Converter x:Key="converter"></wpfApplication3:DivideBy20Converter>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBox FontSize="{Binding ElementName=Window, Path=Width, Converter={StaticResource converter}}"></TextBox>
</Grid>
</Window>
是,通过实施
对于转换器,您的场景将如下所示:
[ValueConversion(typeof(double), typeof(double))]
public class DivideBy20Converter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var f = (double) value;
return f/20.0;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
var f = (double)value;
return f * 20.0;
}
}
。。。在XAML中是这样的:
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wpfApplication3="clr-namespace:WpfApplication3"
Title="MainWindow" Height="350" Width="525"
x:Name="Window">
<Window.Resources>
<wpfApplication3:DivideBy20Converter x:Key="converter"></wpfApplication3:DivideBy20Converter>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBox FontSize="{Binding ElementName=Window, Path=Width, Converter={StaticResource converter}}"></TextBox>
</Grid>
</Window>
您可以使用
IValueConverters
来处理这样的逻辑
下面是您提到的场景示例,您可以绑定到窗口宽度,并使用Converte
r将宽度除以ConverterParameter
r中提供的值
public class MyConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value != null && parameter != null)
{
double divisor = 0.0;
double _val = 0.0;
if (double.TryParse(value.ToString(), out _val) && double.TryParse(parameter.ToString(), out divisor))
{
return _val / divisor;
}
}
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
}
Xaml:
您可以使用
IValueConverters
来处理这样的逻辑
下面是您提到的场景示例,您可以绑定到窗口宽度,并使用Converte
r将宽度除以ConverterParameter
r中提供的值
public class MyConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value != null && parameter != null)
{
double divisor = 0.0;
double _val = 0.0;
if (double.TryParse(value.ToString(), out _val) && double.TryParse(parameter.ToString(), out divisor))
{
return _val / divisor;
}
}
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
}
Xaml:
UseIValueConverter
concept:UseIValueConverter
concept:it's好笑,问了几个小时后,我的老板在我们的培训课程中介绍了这一点。谢谢你的回复,我一定会利用这些。很有趣,在问了几个小时后,我的老板在我们的培训课程中谈到了这一点。谢谢你的回复,我一定会利用这些。