与Converter Parameter绑定错误,当使用system:Object数组时,使用系统数组:Brush

时间:2014-10-30 17:32:45

标签: c# wpf xaml

我正在尝试将一个系统数组:Object作为参数传递给我的转换器: Xaml:不起作用

<TextBlock.Text>
    <Binding ElementName="MainGrid" Path="DataContext"  Converter="{StaticResource TestConverter}">
        <Binding.ConverterParameter>
            <x:Array Type="system:Object">
                <Binding ElementName="MainGrid" Path="DataContext" />
                <Binding ElementName="SomeOtherElement" Path="DataContext" />
            </x:Array>
        </Binding.ConverterParameter>
    </Binding>
</TextBlock.Text>

在XAML之后工作,我在网上发现了一个使用Brush数组的示例:

<TextBlock.Text>
    <Binding ElementName="MainGrid" Path="DataContext"  Converter="{StaticResource TestConverter}">
        <Binding.ConverterParameter>
            <x:Array Type="Brush">
                <SolidColorBrush Color="LawnGreen"/>
                <SolidColorBrush Color="LightSkyBlue"/>
                <SolidColorBrush Color="LightCoral"/>
            </x:Array>
        </Binding.ConverterParameter>
    </Binding>
</TextBlock.Text>

我得到一个System.Windows.Markup.XamlParseException:'Binding'不能在'ArrayList'集合中使用。 '绑定'只能在DepedencyProperty或DependencyObject上设置。

我尝试了一个建议的答案,例如将ViewModel作为依赖项对象添加到我的转换器但是无法正常工作

public class TestConverter : DependencyObject , IValueConverter  
{
    public static readonly DependencyProperty PropertyTypeProperty = DependencyProperty.Register(
        "PropertyType", typeof (DerivedRacingViewModel), typeof (TestConverter), new PropertyMetadata(default(DerivedRacingViewModel)));

    public DerivedRacingViewModel PropertyType
    {
        get { return (DerivedRacingViewModel) GetValue(PropertyTypeProperty); }
        set { SetValue(PropertyTypeProperty, value); }
    }


    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var x = parameter;
        return "Test";
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var x = parameter;
        var z = parameter;
        throw new NotImplementedException();
    }
}

然后将我的xaml更改为:

<converters:TestConverter x:Key="TestConverter"  DerivedRacingViewModel="{Binding}" />

这给我编译时错误: 在'TestConverter'类型中找不到'DerivedRacingViewModel'。

这样做的原因是我想在我的ConvertBack上有2或3个对象,例如我需要输入文本框的文本,文本框绑定的值和视图模型。这是我遇到真正困难的地方。我已经看到其他人通过拆分字符串和东西来做这件事,但我真的不喜欢它。

1 个答案:

答案 0 :(得分:0)

您应该使用下面的ItemsControl

<TextBlock.Text>
    <Binding ElementName="MainGrid" Path="DataContext"  Converter="{StaticResource TestConverter}">
        <Binding.ConverterParameter>
               <ItemsControl>
                  <ItemsControl.Items>
                     <Label Content="{Binding ElementName=MainGrid, Path=DataContext}"/>
                     <Label Content="{Binding ElementName=SomeOtherElement, Path=DataContext}"/>
                  </ItemsControl.Items>
               </ItemsControl>
        </Binding.ConverterParameter>
    </Binding>
</TextBlock.Text>

TestConverter

public class TestConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        ItemsControl ctrl = parameter as ItemsControl;
        Label lbl = ctrl.Items[0] as Label;
        var c = lbl.Content;

        ...
    }