问题描述
我想实现一个WPF用户控件结合一个文本框,使用转换器双打的名单。如何设置用户控件的实例是转换器的参数?
在code为控制如下所示
感谢
<用户控件X:类=BaySizeControl.BaySizeTextBox
的xmlns =http://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
的xmlns:X =http://schemas.microsoft.com/winfx/2006/xaml
XMLNS:地方=CLR的命名空间:BaySizeControl
>
< UserControl.Resources>
<地方:BayListtoStringConverter X:关键=BaySizeConverter/>
< /UserControl.Resources>
<电网>
<文本框名称=Textbox_baysizes
文本={绑定的RelativeSource = {的RelativeSource自我},
路径= Parent.Parent.BaySizeItemsSource,
变换器= {的StaticResource BaySizeConverter}}
/>
< /网格>
< /用户控件>
该参数是需要你的转换常数。为了提供一个对象实例到你的转换器,你可以使用MultiBinding。
注:对于此解决方案的工作,你还需要修改你的转换器来实现IMultiValueConverter代替的IValueConverter。幸运的是,所涉及的修改是相当小。您将可以添加一个验证你的情况提供给你的转换器,2值的数量。
<文本框名称=Textbox_baysizes>
< TextBox.Text>
< MultiBinding转换器={的StaticResource BaySizeConverter}>
<绑定的RelativeSource ={的RelativeSource自我}路径=Parent.Parent.BaySizeItemsSource/>
<绑定的ElementName =Textbox_baysizes/>
< / MultiBinding>
< /TextBox.Text>
< /文本框>
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>
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.
<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>
这篇关于我应该转换参数是此绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!