【问题标题】:c# wpf - Binding to two ElementNamec# wpf - 绑定到两个 ElementName
【发布时间】:2015-10-07 05:34:43
【问题描述】:

我想将 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>

【问题讨论】:

    标签: c# wpf binding telerik


    【解决方案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>    
    

    【讨论】:

    • @Mirza 我在回答中修改了 XAML 以向您展示我在说什么
    • 有一个错误The type 'local:MultiValueConverter' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built 我将本地设置为我的命名空间
    • @Mirza:显然,你必须在资源中定义转换器。
    • 没有必要在资源中定义转换器,但这种方法应该可行。这是一个标记扩展,所以像 Converter="{local:MultiValueConverter}" 这样的设置器应该是正确的。您是否尝试过重建您的项目?您还可以使用 之类的转换器。我还建议您检查是否在正确的命名空间中声明了转换器。
    • @Alexis 是的,它在正确的命名空间中,我把它放在后面的代码中,现在有这个错误'{local:MultiValueConverter}' value is not a valid MarkupExtension expression. Cannot resolve 'MultiValueConverter' in namespace 'clr-namespace:DSSchedule'. 'MultiValueConverter' must be a subclass of MarkupExtension.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-06
    • 2016-11-03
    • 2014-06-07
    相关资源
    最近更新 更多