我应该转换参数是此绑定绑定、参数

2023-09-03 12:41:44 作者:你的爱情、并不属于我

我想实现一个WPF用户控件结合一个文本框,使用转换器双打的名单。如何设置用户控件的实例是转换器的参数?

在code为控制如下所示

感谢

 <用户控件X:类=BaySizeControl.BaySizeTextBox
    的xmlns =htt​​p://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
    的xmlns:X =htt​​p://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>
< /文本框>
 

02.参数绑定及自定义类型转换

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>