【问题标题】:Bind based on a condition基于条件绑定
【发布时间】:2012-11-01 04:03:02
【问题描述】:

嗨。我遇到了在RadListBox 中有RadExpander 的问题。基本上,我有类似herehere 的东西。

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

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

编辑:(让一些反对者高兴的示例代码)

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

【问题讨论】:

  • 叫我懒惰,但是将实际的代码示例放在您自己的问题中而不是链接到其他问题中会更有利于您;特别是您尝试使用的代码。同样,您尝试触发的条件是什么?
  • @Shankar 你需要提供更多关于你想要什么的信息、示例或一些代码。
  • 万岁 Stackoverflow!这太过分了。该网站的创建是为了帮助社区解决技术难题。如果我用一些代码开始这个问题怎么办?你仍然会否决它,因为它是多余的。我搜索了论坛,想出了一些答案,在我的问题中发布了 URL,然后期待一些稍微不同的需求的解决方案。我将在此处发布代码,但它与我链接的代码完全相同。

标签: c# wpf binding listbox expander


【解决方案1】:

解决方案 1. 响应展开事件并通过代码更改元素的绑定。

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

【讨论】:

  • 谢谢乔,我有你建议的解决方法。但是,我只是希望有一种更清洁的方式
  • @Shankar - 一种更简洁的方法是通过 XAML 使用 Trigger。通过查看 IsExpanded == true/false,您可以设置一个或另一个绑定,就像设置颜色或透明度一样。
  • 嗨,乔,我试过了。但它不起作用,因为 IsSelected 不是依赖属性。这里也有描述:stackoverflow.com/a/11199811/640607
  • 其实你有点不对。您不能这样做的原因(并且在您链接的帖子中进行了解释)是因为您无法通过 Setter 更改绑定。我自己并没有意识到这一点。
  • 我用 MultiBinding 的想法编辑了我的帖子。虽然实际的代码隐藏逻辑与您的事件逻辑相似,但对于阅读您的代码的任何人来说,这个扩展器的绑定正在发生的事情会更加明显。
猜你喜欢
  • 1970-01-01
  • 2010-10-30
  • 1970-01-01
  • 2013-02-22
  • 1970-01-01
  • 2019-11-08
  • 2020-04-13
  • 1970-01-01
  • 2019-12-26
相关资源
最近更新 更多