此绑定的转换器参数应该是什么
我正在尝试实现一个 wpf 用户控件,该控件使用转换器将文本框绑定到双精度列表。 如何将用户控件的实例设置为转换器参数?
控件的代码如下所示
谢谢
<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>
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?
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
另一种方法是让转换器继承 DependencyObject(或 FrameworkElement)。 这允许您声明依赖属性,从而可以从 XAML(甚至是 Binding)设置其值。
示例:用于乘以指定因子的值的转换器,该因子是从自定义控件 (MyControl) 中的属性 (FactorValue) 获取的。
转换器:
在 XAML 中使用:
因此,您现在可以为转换器中需要的每个参数声明一个依赖属性并绑定它。
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.
Example: A converter to multiply a value specifing the factor, which is obtained from a property (FactorValue) in a custom control (MyControl).
The converter:
Use in XAML:
So, you can now declare a dependency property for each parameter you need in your converter and bind it.
这些参数是转换器所需的常量。 要向转换器提供对象实例,您可以使用 MultiBinding。
注意:要使此解决方案发挥作用,您还需要修改转换器以实现 IMultiValueConverter 而不是 IValueConverter。 幸运的是,涉及的修改相当少。 您可以添加对提供给转换器的值数量的验证,在您的情况下为 2。
The parameters are for constants needed by your converter. To provide an object instance to your converter, you can use 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.
我将命名该控件,然后使用 ElementName 进行绑定:
不,等等,这不起作用,因为 ConverterParameter 不是依赖项属性,绑定也不是 DependencyObject。 ReleativeSource 标记扩展应该执行您想要的操作,尽管我没有将其嵌套在其他 MarkupExtension 中 - 也许在这种情况下它的表现不佳:
I would name the control and then bind using ElementName:
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:
我遇到了同样的问题,但我无法使用 MultiBindings,因为我需要正确实现 ConvertBack 方法。 这是我最终为 CheckBox 的 IsChecked 属性实现的解决方案:
我不太熟悉 TemplateBindings (或与此相关的任何 WPF),所以也许这只有效,因为我的 CheckBox 位于 DataTemplate 中...
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:
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...