c#wpf - 绑定到两个ElementName

时间:2015-10-07 05:34:44

标签: c# wpf binding telerik

我想将RadCalendar中的SelectedDate绑定到不同RadScheduleView中的两个CurrentDate。我怎么能这样做?

<telerik:RadCalendar Name="radCalendar"
    Canvas.Left="80" Canvas.Top="200" 
    Height="320" Width="400"
    SelectedDate="{Binding CurrentDate, ElementName=radScheduleView,  Mode=TwoWay}"
    SelectionMode="Single" DisplayDate="{Binding DisplayDate, Mode=TwoWay}">
</telerik:RadCalendar>

我希望有两个ElementName=radScheduleViewElementName=radScheduleView1

修改

这里是我需要绑定的代码

<telerik:RadCalendar Name="radCalendar"
     Canvas.Left="80" Canvas.Top="200" 
     Height="320" Width="400"
     SelectionMode="Single" DisplayDate="{Binding DisplayDate, Mode=TwoWay}" >

     <telerik:RadCalendar.SelectedDate>
          <MultiBinding Converter="MultiValueConverter" Mode="TwoWay">
              <Binding ElementName="radScheduleView" Path="CurrentDate"/>
              <Binding ElementName="radScheduleView1" Path="CurrentDate"/>
          </MultiBinding>
     </telerik:RadCalendar.SelectedDate>

<telerik:RadScheduleView Name="radScheduleView1" 
      Canvas.Left="60" Canvas.Top="130" 
      Height="420" Width="570"
      AppointmentsSource="{Binding Appointments}"
      SelectedAppointment="{Binding SelectedAppointment, Mode=TwoWay}"
      ActiveViewDefinitionIndex="{Binding ActiveViewDefinitionIndex,Mode=TwoWay}"
      CurrentDate="{Binding CurrentDate, Mode=TwoWay}">

      <telerik:RadScheduleView.ViewDefinitions>
         <telerik:DayViewDefinition MinorTickLength="10min" />
         </telerik:RadScheduleView.ViewDefinitions>
      </telerik:RadScheduleView>

<telerik:RadScheduleView Name="radScheduleView" 
     Canvas.Left="60" Canvas.Top="130" 
     Height="420" Width="570"
     AppointmentsSource="{Binding Appointments}"
     SelectedAppointment="{Binding SelectedAppointment, Mode=TwoWay}"
     ActiveViewDefinitionIndex="{Binding ActiveViewDefinitionIndex,Mode=TwoWay}"
     CurrentDate="{Binding CurrentDate, Mode=TwoWay}">

     <telerik:RadScheduleView.ViewDefinitions>
          <telerik:DayViewDefinition MinorTickLength="1h" />
      </telerik:RadScheduleView.ViewDefinitions>

1 个答案:

答案 0 :(得分:1)

您可以使用MultiBinding代替简单Binding,并将其与IMultiValueConverter实施一起使用,如下所示:

public class MultiValueConverterExtension : MarkupExtension, IMultiValueConverter {
    public override object ProvideValue(IServiceProvider serviceProvider) {
        return this;
    }
    object IMultiValueConverter.Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
        return values[1];
    }

    object[] IMultiValueConverter.ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) {
        return new object[] { value, value };
    }
}

在这种情况下,XAML将是这样的:

<telerik:RadCalendar Name="radCalendar"
    Canvas.Left="80" Canvas.Top="200" 
    Height="320" Width="400"
    SelectionMode="Single" DisplayDate="{Binding DisplayDate, Mode=TwoWay}">
    <telerik:RadCalendar.SelectedDate>
        <MultiBinding Converter="{local:MultiValueConverter}" Mode="TwoWay">
             <Binding ElementName="radScheduleView" Path="CurrentDate"/>
             <Binding ElementName="radScheduleView1" Path="CurrentDate"/>
        </MultiBinding>
    </telerik:RadCalendar.SelectedDate>
</telerik:RadCalendar>