是否在DataTemplate中选择了元素的可见性

时间:2014-02-03 19:58:49

标签: .net wpf visibility datatemplate

此DataTemplate用于ListBox中 希望fieldTemplateDetail仅在ListBoxItem IsSelected

时显示
Visibility="{Binding Path=IsSelected, Converter=bvc}"   

以上不是有效的语法,但这就是我想要做的事情

<DataTemplate x:Key="fieldTemplate">
    <Border BorderBrush="Orange" BorderThickness="2" Margin="2" Padding="2">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="60"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding Path=Name}" />
            <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Path=DisplayValue}" />
            <ContentPresenter Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" 
                        x:Name="fieldTemplateDetail"
                        Content="{Binding}"
                        Visibility="{Binding Path=IsSelected, Converter=bvc}"
                        ContentTemplateSelector="{StaticResource fieldTemplateSelector}"/>
        </Grid>
    </Border>
</DataTemplate>

我还没有看过III的答案,但这适用于详细模板
但显然我不想重复所有的细节控制

<DataTemplate x:Key="fieldStringTemplate">            
    <StackPanel x:Name="fieldString" Visibility="Collapsed">
        <TextBox Text="{Binding Path=FieldValue}" />
    </StackPanel>
    <DataTemplate.Triggers>
        <DataTrigger
            Binding="{Binding
            RelativeSource={RelativeSource
                Mode=FindAncestor,
                AncestorType={x:Type ListBoxItem}},
                Path=IsSelected}"
                Value="True">
            <Setter TargetName="fieldString" Property="Visibility" Value="Visible"/>
        </DataTrigger>
    </DataTemplate.Triggers>
</DataTemplate>

III的答案是抛出错误 能见度
TypeConverter IValueConverter不支持从字符串转换

<ContentPresenter Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" 
            x:Name="fieldTemplateDetail"
            Visibility="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem},
                            Path=IsSelected, Converter=bvc}"
            Content="{Binding}"
            ContentTemplateSelector="{StaticResource fieldTemplateSelector}"/>

这似乎有效

<local:FieldTemplateSelector x:Key="fieldTemplateSelector"/>
<DataTemplate x:Key="fieldTemplate">
    <Border BorderBrush="Orange" BorderThickness="2" Margin="2" Padding="2">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="60"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding Path=Name}" />
            <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Path=DisplayValue}" />
            <ContentPresenter Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2"
                        Visibility="Collapsed"
                        x:Name="fieldTemplateDetail"
                        Content="{Binding}"
                        ContentTemplateSelector="{StaticResource fieldTemplateSelector}"/>
        </Grid>
    </Border>
    <DataTemplate.Triggers>
        <DataTrigger
            Binding="{Binding
            RelativeSource={RelativeSource
                Mode=FindAncestor,
                AncestorType={x:Type ListBoxItem}},
                Path=IsSelected}"
                Value="True">
            <Setter TargetName="fieldTemplateDetail" Property="Visibility" Value="Visible"/>
        </DataTrigger>
    </DataTemplate.Triggers>
</DataTemplate>

1 个答案:

答案 0 :(得分:1)

更新您对此的绑定

<ContentPresenter Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" 
                  x:Name="fieldTemplateDetail"
                  Content="{Binding}"
                  Visibility="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem},
                               Path=IsSelected, Converter={StaticResource bvc}}"
                  ContentTemplateSelector="{StaticResource fieldTemplateSelector}"/>

更新我必须添加StaticResource才能找到转换器。