.net 此绑定的转换器参数应该是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/377841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What should the converter parameter be for this binding
提问by Dave Turvey
I am trying to implement a wpf user control that binds a text box to a list of doubles using a converter. How can i set the instance of user control to be the converter parameter?
我正在尝试实现一个 wpf 用户控件,该控件使用转换器将文本框绑定到双打列表。如何将用户控件的实例设置为转换器参数?
the code for the control is shown below
控件的代码如下所示
Thanks
谢谢
<UserControl x:Class="BaySizeControl.BaySizeTextBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:BaySizeControl"
>
<UserControl.Resources>
<local:BayListtoStringConverter x:Key="BaySizeConverter"/>
</UserControl.Resources>
<Grid>
<TextBox Name="Textbox_baysizes"
Text="{Binding RelativeSource={RelativeSource self},
Path=Parent.Parent.BaySizeItemsSource,
Converter={StaticResource BaySizeConverter}}"
/>
</Grid>
</UserControl>
采纳答案by Frederic
The parameters are for constants needed by your converter. To provide an object instance to your converter, you can use MultiBinding.
这些参数用于转换器所需的常量。要向转换器提供对象实例,您可以使用 MultiBinding。
Note: For this solution to work, you also need to modify your converter to implement IMultiValueConverter instead of IValueConverter. Fortunately, the modifications involved are fairly little. You will can add a validation for the number of values provided to your converter, 2 in your case.
注意:要使此解决方案起作用,您还需要修改转换器以实现 IMultiValueConverter 而不是 IValueConverter。幸运的是,所涉及的修改相当少。您可以为提供给转换器的值数量添加验证,在您的情况下为 2。
<TextBox Name="Textbox_baysizes">
<TextBox.Text>
<MultiBinding Converter="{StaticResource BaySizeConverter}">
<Binding RelativeSource="{RelativeSource self}" Path="Parent.Parent.BaySizeItemsSource"/>
<Binding ElementName="Textbox_baysizes"/>
</MultiBinding>
</TextBox.Text>
</TextBox>
回答by JoanComasFdz
Another way is making your converter inherit from DependencyObject (or FrameworkElement). This allows you to declare dependency properties, making it possible to set its values from XAML, even a Binding.
另一种方法是让您的转换器从 DependencyObject(或 FrameworkElement)继承。这允许您声明依赖项属性,从而可以从 XAML 设置其值,甚至是 Binding。
Example: A converter to multiply a value specifing the factor, which is obtained from a property (FactorValue) in a custom control (MyControl).
示例:用于乘以指定因子的值的转换器,该值是从自定义控件 (MyControl) 中的属性 (FactorValue) 获得的。
The converter:
转换器:
public class MyConverter : DependencyObject, IValueConverter
{
// The property used as a parameter
public double Factor
{
get { return (double) GetValue(FactorProperty); }
set { SetValue(FactorProperty, value); }
}
// The dependency property to allow the property to be used from XAML.
public static readonly DependencyProperty FactorProperty =
DependencyProperty.Register(
"Factor",
typeof(double),
typeof(MyConverter),
new PropertyMetadata(1.15d));
#region IValueConverter Members
object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
// Use the property in the Convert method instead of "parameter"
return (double) value * Factor;
}
object IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
Use in XAML:
在 XAML 中使用:
<MyConverter x:Key="MyConv"
Factor={Binding ElementName=MyControl, Path=FactorValue}
/>
So, you can now declare a dependency property for each parameter you need in your converter and bind it.
因此,您现在可以为转换器中需要的每个参数声明一个依赖属性并绑定它。
回答by Daniel Paull
I would name the control and then bind using ElementName:
我会命名控件,然后使用 ElementName 进行绑定:
<UserControl x:Class="BaySizeControl.BaySizeTextBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:BaySizeControl"
Name="Foobar"
>
<UserControl.Resources>
<local:BayListtoStringConverter x:Key="BaySizeConverter"/>
</UserControl.Resources>
<Grid>
<TextBox Name="Textbox_baysizes"
Text="{Binding RelativeSource={RelativeSource self},
Path=Parent.Parent.BaySizeItemsSource,
Converter={StaticResource BaySizeConverter,
ConverterParameter={Binding ElementName=Foobar} }}"
/>
</Grid>
</UserControl>
No, wait, that won't work because the ConverterParameter is not a Dependency Property, nor is the Binding a DependencyObject. A ReleativeSource markup extension should do what you want, though I've not used it nested inside other MarkupExtension - perhaps it is not well behaved in this case:
不,等等,这将不起作用,因为 ConverterParameter 不是依赖属性,绑定也不是 DependencyObject。ReleativeSource 标记扩展应该做你想做的,虽然我没有使用它嵌套在其他 MarkupExtension 中 - 也许在这种情况下它表现不佳:
<TextBox Name="Textbox_baysizes"
Text="{Binding RelativeSource={RelativeSource self},
Path=Parent.Parent.BaySizeItemsSource,
Converter={StaticResource BaySizeConverter,
ConverterParameter={RelativeSource self} }}"
/>
回答by Nicolas Rousseau-Dupuis
I had the same problem, but I can't use MultiBindings since I need to correctly implement the ConvertBack method. Here is the solution I ended up implementing for a CheckBox's IsChecked property:
我遇到了同样的问题,但我不能使用 MultiBindings,因为我需要正确实现 ConvertBack 方法。这是我最终为 CheckBox 的 IsChecked 属性实现的解决方案:
<CheckBox>
<CheckBox.IsChecked>
<Binding Converter="{StaticResource myConverter}" Path="Value">
<Binding.ConverterParameter>
<FrameworkElement DataContext="{TemplateBinding DataContext}" />
</Binding.ConverterParameter>
</Binding>
</CheckBox.IsChecked>
</CheckBox>
I'm not super familiar with TemplateBindings (or anything WPF for that matter), so maybe this only works because my CheckBox is in a DataTemplate...
我对 TemplateBindings(或任何 WPF 与此相关的东西)不是很熟悉,所以这可能只是因为我的 CheckBox 位于 DataTemplate 中...

