【问题标题】:Binding error with Converter Parameter, when uses an array of system:Object, works with array of system:Brush转换器参数的绑定错误,当使用系统数组时:对象,使用系统数组:画笔
【发布时间】:2014-10-30 17:32:45
【问题描述】:

我正在尝试将 system: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:“绑定”不能在“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 个对象可用,例如我需要输入到文本框中的文本、文本框绑定的值和视图模型。这是我遇到真正困难的地方。我见过其他人通过拆分字符串和其他东西来做到这一点,但我真的不喜欢那样。

【问题讨论】:

  • 不确定它是如何工作的?您无法在parameter 参数中获取绑定数组?是否有任何异常或错误(在 Output 窗口中通知)?请注意,XAML 中的Binding 具有非常复杂的实现。
  • 该异常意味着您不能像那样使用Binding。正如我所说,它非常复杂,而不是某种普通的实例。我建议不要以这种方式创建 Binding 实例,而应尝试仅传递 ElementNamePath (所有这些都只是字符串),然后在代码隐藏中,您可以尝试根据这些输入创建 Binding 实例。
  • 那个属性当然没有找到,只是一个Type,正确的属性是PropertyType
  • D'oh,我真的很愚蠢,正在使用 resharper 并没有完成,真的很累一直在努力解决这个问题。
  • 放心,我对这个话题很感兴趣,试试看,如果有帮助我会投赞成票:)

标签: c# wpf xaml


【解决方案1】:

您应该使用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>

测试转换器

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;

        ...
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-27
    • 2017-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多