简体   繁体   中英

c# wpf - Binding to two ElementName

I want to bind the SelectedDate in RadCalendar, to two CurrentDate in different RadScheduleView. How can i do that?

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

I want to have two ElementName=radScheduleView and ElementName=radScheduleView1

EDIT

here's my code that need to be bound

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

You can use MultiBinding instead of simple Binding and use it with the IMultiValueConverter implementation like the following one:

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

In this case XAML will be like this one:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM