代码之家  ›  专栏  ›  技术社区  ›  David Turvey

此绑定的转换器参数应该是什么

  •  10
  • David Turvey  · 技术社区  · 16 年前

    我试图实现一个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>
    
    4 回复  |  直到 12 年前
        1
  •  11
  •   JoanComasFdz    13 年前

    另一种方法是使转换器从DependencyObject(或FrameworkElement)继承。这允许您声明依赖属性,从而可以从XAML甚至绑定设置其值。

    示例:用于乘以指定因子的值的转换器,该值是从自定义控件(MyControl)中的属性(FactorValue)中获得的。

    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
    }
    

    <MyConverter x:Key="MyConv"
                 Factor={Binding ElementName=MyControl, Path=FactorValue}
    />
    

    因此,您现在可以为转换器中需要的每个参数声明一个依赖项属性并绑定它。

        2
  •  8
  •   Frederic    16 年前

    这些参数用于转换器所需的常量。要向转换器提供对象实例,可以使用多重绑定。

    注意:要使此解决方案起作用,还需要修改转换器以实现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>
    
        3
  •  1
  •   Daniel Paull    16 年前

    <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>
    

    不,等等,这不起作用,因为ConverterParameter不是依赖属性,绑定也不是DependencyObject。RelativeSource标记扩展应该做您想做的事情,尽管我没有在其他MarkupExtension中嵌套使用它-在这种情况下,它可能表现不好:

    <TextBox  Name="Textbox_baysizes" 
                          Text="{Binding RelativeSource={RelativeSource self},
                                        Path=Parent.Parent.BaySizeItemsSource, 
                                        Converter={StaticResource BaySizeConverter,
                                        ConverterParameter={RelativeSource self} }}"
                          />
    
        4
  •  1
  •   Nicolas Rousseau-Dupuis    14 年前

    <CheckBox>
        <CheckBox.IsChecked>
            <Binding Converter="{StaticResource myConverter}" Path="Value">
                <Binding.ConverterParameter>
                    <FrameworkElement DataContext="{TemplateBinding DataContext}" />
                </Binding.ConverterParameter>
            </Binding>
        </CheckBox.IsChecked>
    </CheckBox>
    

    我不太熟悉TemplateBindings(或者任何WPF),所以这可能只是因为我的复选框在DataTemplate中才起作用。。。