2012-11-01 78 views
0

嗨。我遇到了在RadListBox中有RadExpander的问题。基本上,我有类似herehere根据条件绑定

我有一个OneWayToSource绑定,但我希望绑定仅在RadExpander展开时发生,而不是在展开时发生。

有没有一种方法可以有条件地绑定两个UI元素?

编辑:(示例代码,这使得一些快乐的downvoters的)

<DataTemplate x:Key="ListBoxItemTemplate" DataType="{x:Type telerik:RadListBoxItem}"> 
    <!--<DataTemplate.Triggers> 
     <DataTrigger Binding="{Binding ElementName=listExpander, Path=IsExpanded, Mode=TwoWay}" Value="True"> 
      <Setter Property="IsSelected" Value="True" /> 
     </DataTrigger> 
    </DataTemplate.Triggers>--> 
    <Grid> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition /> 
      <ColumnDefinition /> 
     </Grid.ColumnDefinitions> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="Auto" /> 
     </Grid.RowDefinitions> 
     <telerik:RadExpander x:Name="listExpander" 
          IsExpanded="{Binding Mode=TwoWay, IsAsync=True, Path=IsSelected, RelativeSource={RelativeSource AncestorType=telerik:RadListBoxItem, Mode=FindAncestor}}" 
          VerticalContentAlignment="Top" ToolTip="Double click on the List name to edit it"> 
      <telerik:RadExpander.Header> 
       <Grid> 
        <TextBlock x:Name="listNameCaption" MouseDown="listName_txtblk_MouseDown" 
           Text="{Binding ListName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource HighlightedDetailsStyleForTextBlock}" /> 
        <TextBox LostFocus="listName_txtbox_LostFocus" Visibility="Collapsed" 
          Text="{Binding ListName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
          Style="{StaticResource HighlightedDetailsStyleForTextBox}" /> 
       </Grid> 
      </telerik:RadExpander.Header> 
      <telerik:RadExpander.Content> 
       <Grid> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition /> 
        </Grid.ColumnDefinitions> 
        <Grid.RowDefinitions> 
         <RowDefinition Height="Auto" /> 
         <RowDefinition Height="Auto" /> 
         <RowDefinition Height="Auto" /> 
        </Grid.RowDefinitions> 
        <Border BorderBrush="#FFDADADA" BorderThickness="0,0,1,1" MinHeight="20" MinWidth="200" CornerRadius="3" Margin="5"> 
         <Border BorderBrush="#B2ADBDD1" BorderThickness="1" CornerRadius="2"> 
          <StackPanel> 
           <StackPanel Orientation="Horizontal"> 
            <Label FontFamily="Segoe UI" FontSize="11" Content="List Type:" FontStyle="Italic" /> 
            <Label FontFamily="Segoe UI" FontSize="11" Content="{Binding ListType}" /> 
           </StackPanel> 
           <StackPanel Orientation="Horizontal"> 
            <Label FontFamily="Segoe UI" FontSize="11" Content="Tree:" FontStyle="Italic" /> 
            <Label FontFamily="Segoe UI" FontSize="11" Content="{Binding TreeName}" /> 
           </StackPanel> 
           <StackPanel Orientation="Horizontal"> 
            <Label FontFamily="Segoe UI" FontSize="11" Content="Discount Date:" FontStyle="Italic" /> 
            <Label FontFamily="Segoe UI" FontSize="11" Content="{Binding DiscountDate}" /> 
           </StackPanel> 
          </StackPanel> 
         </Border> 
        </Border> 
       </Grid> 
      </telerik:RadExpander.Content> 
      <!--<telerik:RadExpander.Style> 
       <Style TargetType="{x:Type telerik:RadExpander}"> 
        <Style.Triggers> 
         <Trigger Property="IsExpanded" Value="True"> 
          <Setter Property="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem, Mode=FindAncestor}, Path=IsSelected}" Value="True"/> 
         </Trigger> 
        </Style.Triggers> 
       </Style> 
      </telerik:RadExpander.Style>--> 
     </telerik:RadExpander> 
    </Grid> 
</DataTemplate> 

<telerik:RadListBox x:Name="allListBox" 
        Style="{StaticResource ListBoxStyle}" 
        ItemsSource="{Binding Lists, Mode=TwoWay}" 
        ItemTemplate="{StaticResource ListBoxItemTemplate}" 
        SelectedItem="{Binding SelectedListItem, Mode=TwoWay}"> 
    <telerik:RadListBox.ItemsPanel> 
     <ItemsPanelTemplate> 
      <VirtualizingStackPanel Orientation="Vertical" /> 
     </ItemsPanelTemplate> 
    </telerik:RadListBox.ItemsPanel> 
</telerik:RadListBox> 

编辑2

固定它,但是从后面的代码。希望有一种更简洁的方式,我们可以说:“RadExpander - 只有在XAML中有IsExpanded = True时,才能绑定到RadListBoxItem的IsSelected属性”。

这是变通代码:

private void ListItemExpanded(object sender, RadRoutedEventArgs e) 
{ 
    var listItem = sender as RadExpander; 
    if(listItem == null) 
     return; 

    if (!listItem.IsExpanded) return; 


    var parent = VisualTreeHelper.GetParent(listItem); 
    while (parent != null && !(parent is RadListBoxItem)) 
    { 
     parent = VisualTreeHelper.GetParent(parent); 
    } 

    if(parent == null) 
     return; 

    var listBoxItem = parent as RadListBoxItem; 
    if (!listBoxItem.IsSelected) 
     listBoxItem.IsSelected = true; 
} 

<telerik:RadExpander x:Name="listExpander" Expanded="ListItemExpanded" VerticalContentAlignment="Top" ToolTip="Double click on the List name to edit it"> 
+1

叫我懒,但它会多了很多有利于你把实际的代码样本中反对链接到别人你自己的问题;特别是你正在努力工作的代码。在同一张纸条上,你试图触发的条件是什么? – newfurniturey

+0

@Shankar你需要提供更多关于你想要的信息, 的例子或一些代码。 –

+0

冰雹Stackoverflow!这太过分了。该网站的创建目的是为了帮助有技术困难的社区。如果我用一些代码开始提问,该怎么办?你仍然会低估它,因为它是多余的。我搜索了论坛,想出了一些答案,在我的问题中发布了网址,然后期望有一些稍有不同要求的解决方案。我将在这里发布代码,但它将与我链接的完全相似。 – Shankar

回答

1

解决方法1.反应展开事件,并通过代码更改元素的结合。

解决方案2:使用MultiBinding和IMultiValueConverter:http://www.switchonthecode.com/tutorials/wpf-tutorial-using-multibindings

+0

谢谢乔,我有你提出的解决方法。不过,我只希望有一个更清晰的方式 – Shankar

+0

@Shankar - 一个更简洁的方法是通过XAML使用[Trigger](http://wpftutorial.net/Triggers.html)。通过查看IsExpanded == true/false,您可以设置一个绑定或另一个绑定,就像设置颜色或透明度一样。 – Joe

+0

嗨,乔,我尝试过。但它不起作用,因为IsSelected不是一个依赖属性。它也在这里描述:http://stackoverflow.com/a/11199811/640607 – Shankar