Xaml - 在FallBackValue中绑定

时间:2015-05-12 17:01:10

标签: c# xaml silverlight binding telerik

如何在网格中的列上创建两个绑定,如果第一个绑定为空或为null,将使用第二个绑定。我试图用FallbackValue属性做到这一点,但是你不能在其中仅使用静态值进行绑定。 这里有一些代码,不仅仅是单词!

<telerik:RadGridView x:Name="radGridView">
     <telerik:RadGridView.Columns>
        <telerik:GridViewDataColumn DataMemberBinding="{Binding FirstName,
            FallbackValue=Binding FirstName2}"  //You cant do that!
            Header="First Name" />
     </telerik:RadGridView.Columns>
</telerik:RadGridView>

必须有一些在xaml中做到这一点!请帮忙!

2 个答案:

答案 0 :(得分:0)

你可以使用DataTriggers,像这样的东西

<telerik:RadGridView x:Name="radGridView">
  <telerik:RadGridView.Columns>
    <telerik:GridViewDataColumn Header="First Name">
        <telerik:GridViewDataColumn.Style>
            <Style TargetType="{x:Type telerik:GridViewDataColumn}">
              <Setter Property="DataMemberBinding" Value="{Binding FirstName}"/>
              <Style.Triggers>
                  <DataTrigger Binding="{Binding FirstName}" Value="{x:Null}">
                      <Setter Property="DataMemberBinding" Value="{Binding FirstName2}"/>
                  </DataTrigger>
              </Style.Triggers>
            </Style>
        </telerik:GridViewDataColumn.Style>
    </telerik:GridViewDataColumn>
  </telerik:RadGridView.Columns>
</telerik:RadGridView>

答案 1 :(得分:0)

我至少可以想到两种方式:

第一个解决方案(在DataContext中解决):DataContext类型中添加一个属性,类似于 ResolvedFirstName ,并且该值来自 FirstName FirstName2 ,当两者中的任何一个发生更改时,将重新评估 ResolvedFirstName

public string ResolvedFirstName {
    get { return string.IsNullOrEmpty(FirstName) ? FirstName2 : FirstName; }
}

public string FirstName {
    get {...}
    set {...
        RaisePropertyChanged("FirstName");
        RaisePropertyChanged("ResolvedFirstName");}
}

public string FirstName2 {
    get {...}
    set {...
        RaisePropertyChanged("FirstName2");
        RaisePropertyChanged("ResolvedFirstName");}
}

第二个解决方案(在视图中解决):添加StringMultiplexer作为StaticResource绑定到这两个属性,它提供输出属性你的专栏可以绑定。

<RadGridView>
    <RadGridView.Resources>
        <StringMultiplexer x:Key="FirstNameMux"
            HighPrioString="{Binding FirstName}"
            LowPrioString="{Binding FirstName2}"/>
    </RadGridView.Resources>
    <RadGridView.Columns>
        <GridViewDataColumn
            DataMemberBinding="{Binding Output, Source={StaticResource FirstNameMux}}"/>

和代码:

public class StringMultiplexer : DependencyObject
{
    public string LowPrioString
    {
        get { return (string) GetValue( LowPrioStringProperty ); }
        set { SetValue( LowPrioStringProperty, value ); }
    }

    public static readonly DependencyProperty LowPrioStringProperty =
        DependencyProperty.Register( "LowPrioString", typeof( string ), typeof( StringMultiplexer ), new PropertyMetadata( DetermineOutput ) );

    public string HighPrioString
    {
        get { return (string) GetValue( HighPrioStringProperty ); }
        set { SetValue( HighPrioStringProperty, value ); }
    }

    public static readonly DependencyProperty HighPrioStringProperty =
        DependencyProperty.Register( "HighPrioString", typeof( string ), typeof( StringMultiplexer ), new PropertyMetadata( DetermineOutput ) );

    public string Output
    {
        get { return (string) GetValue( OutputProperty ); }
        set { SetValue( OutputProperty, value ); }
    }

    public static readonly DependencyProperty OutputProperty =
        DependencyProperty.Register( "Output", typeof( string ), typeof( StringMultiplexer ), null );

    private static void DetermineOutput( DependencyObject d, DependencyPropertyChangedEventArgs e )
    {
        ((StringMultiplexer) d).DetermineOutput();
    }

    private void DetermineOutput()
    {
        Output = (string.IsNullOrEmpty( HighPrioString )) ? LowPrioString : HighPrioString;
    }
}

当我输入第二个解决方案时,我越来越怀疑这个解决方案在您的具体情况下不起作用。我自己使用了StringMultiplexer,它按预期工作。我在你的案例中看到的问题是:GridViewDataColumn不是FrameworkElement(据我记得),而只是一个Column-definition-object,它提供了实例化实际单元格控件或行控件的蓝图,而DataMemberBinding将是最多的可能会针对为其创建行的项对象进行解析。也许您需要从GridViewDataColumn派生并在那里实现多路复用器。