如果单击数据模板中的控件,如何选择ListView行

时间:2011-03-11 00:17:43

标签: c# wpf listview datatemplate

我有一个使用DataTemplate的ListView。我根据特定项的IsSelected属性交换Item DataTemplate。这允许我显示编辑模板和阅读模板。 ListView包含两列。在读取模式下,两列是只读文本框,在编辑模式下,左列是可编辑的TextBox,右列是下拉列表。只要我在读取模式下不直接单击其中一个TextBox,一切都很有效。如果我在控件外部单击,则选择该行时,在控件内部选择时,不会选择该行。这可以防止我在单击其中一个单元格时进入编辑模式。

我在下面粘贴了我的xaml。您可以看到GridBlock和GridEdit样式将由ListView的IsSelected属性控制。这是允许我根据该DP交换DataTemplate(真正隐藏或折叠)的原因。我进一步专门设计了这些样式,以便在值为空且控件没有焦点时允许带水印的文本框。我认为这是我的问题所在,但对于我的生活,我无法想到以声明的方式做到这一点。另外,我是WPF的新手,所以我确信这种情况有一种模式,很难制定一个可以从google或bing返回有意义结果的查询。感谢您提前提供的任何帮助。

以下是我的样式和数据表:

<Style TargetType="{x:Type FrameworkElement}" x:Key="GridBlockStyle">
    <Setter Property="VerticalAlignment" Value="Center" />
    <Setter Property="Visibility" 
            Value="{Binding Path=IsSelected, 
                    RelativeSource={RelativeSource FindAncestor, 
                    AncestorType={x:Type ListViewItem}},
                    Converter={StaticResource boolToVis}, 
                    ConverterParameter=False}" />
</Style>

<Style TargetType="{x:Type FrameworkElement}" x:Key="GridEditStyle">
    <Setter Property="VerticalAlignment" Value="Center" />
    <Setter Property="Visibility" 
            Value="{Binding Path=IsSelected, 
                    RelativeSource={RelativeSource FindAncestor, 
                    AncestorType={x:Type ListViewItem}},
                    Converter={StaticResource boolToVis}, 
                    ConverterParameter=True}" />
</Style>
<Style x:Key="TextBoxReadOnly" TargetType="{x:Type TextBox}" BasedOn="{StaticResource GridBlockStyle}">
    <Setter Property="KeyboardNavigation.TabNavigation" Value="None" />
    <Setter Property="AllowDrop" Value="true" />
    <Setter Property="Background" Value="Transparent"></Setter>
    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
    <Setter Property="VerticalContentAlignment" Value="Stretch" />
    <Setter Property="Padding" Value="8,5,3,3" />
    <Setter Property="BorderThickness" Value="0" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TextBox}">
                <Grid>
                    <Label x:Name="TextPrompt" Content="{TemplateBinding Tag}" Visibility="Collapsed" Focusable="False"  Foreground="Silver"></Label>
                    <ScrollViewer Margin="0" x:Name="PART_ContentHost" Foreground="{DynamicResource OutsideFontColor}" />
                </Grid>
                <ControlTemplate.Triggers>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsFocused" Value="False"></Condition>
                            <Condition Property="Text" Value=""></Condition>
                        </MultiTrigger.Conditions>
                        <MultiTrigger.Setters>
                            <Setter Property="Visibility" TargetName="TextPrompt" Value="Visible"></Setter>
                        </MultiTrigger.Setters>
                    </MultiTrigger>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="Foreground" Value="DimGray" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style x:Key="TextBoxEditable" TargetType="{x:Type TextBox}" BasedOn="{StaticResource GridEditStyle}">
    <Setter Property="KeyboardNavigation.TabNavigation" Value="None" />
    <Setter Property="AllowDrop" Value="true" />
    <Setter Property="Background" Value="Transparent"></Setter>
    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
    <Setter Property="VerticalContentAlignment" Value="Stretch" />
    <Setter Property="Padding" Value="8,5,3,3" />
    <Setter Property="BorderThickness" Value="0" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TextBox}">
                <Grid>
                    <Border x:Name="BorderBase" Background="White" BorderThickness="1.4,1.4,1,1" 
                    BorderBrush="Silver" />
                    <Label x:Name="TextPrompt" Content="{TemplateBinding Tag}" Visibility="Collapsed" Focusable="False"  Foreground="Silver"></Label>
                    <ScrollViewer Margin="0" x:Name="PART_ContentHost" Foreground="{DynamicResource OutsideFontColor}" />
                </Grid>
                <ControlTemplate.Triggers>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsFocused" Value="False"></Condition>
                            <Condition Property="Text" Value=""></Condition>
                        </MultiTrigger.Conditions>
                        <MultiTrigger.Setters>
                            <Setter Property="Visibility" TargetName="TextPrompt" Value="Visible"></Setter>
                        </MultiTrigger.Setters>
                    </MultiTrigger>
                    <Trigger Property="IsFocused" Value="True">
                        <Setter Property="BorderThickness" TargetName="BorderBase" Value="2.4,2.4,1,1"></Setter>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="Foreground" Value="DimGray" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

这是我的ListView视图:

<ListView.View>
    <GridView>

    <GridViewColumn Width="120">
        <GridViewColumnHeader Content="Resource ID" />
        <GridViewColumn.CellTemplate>
        <DataTemplate>
            <Grid>
            <TextBox Margin="3" Tag="Enter Resource ID" Text="{Binding Path=ResourceID, UpdateSourceTrigger=PropertyChanged}"
                    Style="{StaticResource TextBoxReadOnly}" IsReadOnly="True" />
            <TextBox Width="90" Tag="Enter Resource ID"  Margin="3"
                 Style="{StaticResource TextBoxEditable}" Text="{Binding Path=ResourceID, UpdateSourceTrigger=PropertyChanged}" />
            </Grid>
        </DataTemplate>
        </GridViewColumn.CellTemplate>
    </GridViewColumn>

    <GridViewColumn Width="120">
        <GridViewColumnHeader Content="Code" />
        <GridViewColumn.CellTemplate>
        <DataTemplate>
            <Grid>
            <TextBox Margin="3" Tag="Enter Code" Text="{Binding Path=Code}"
                    Style="{StaticResource TextBoxReadOnly}" IsReadOnly="True" />
            <ComboBox Margin="3" Style="{StaticResource GridEditStyle}" 
                  ItemsSource="{Binding Source={StaticResource CodeViewSource}, Mode=OneWay}"
                  SelectedItem="{Binding Path=Code, Mode=TwoWay}"
                  IsSynchronizedWithCurrentItem="False"
                  util:ComboBoxWidthFromItemsBehavior.ComboBoxWidthFromItems="True" />
            </Grid>
        </DataTemplate>
        </GridViewColumn.CellTemplate>
    </GridViewColumn>

    <GridViewColumn Width="120">
        <GridViewColumn.CellTemplate>
        <DataTemplate>
            <Button Margin="5" Content="Delete" 
                Command="{Binding Path=DataContext.RemoveORIEntryCommand,
                RelativeSource={RelativeSource FindAncestor, 
                AncestorType={x:Type UserControl}}}">
            <Button.Resources>
                <Converter:AgencyItemIDParametersConverter x:Key="RemoveListViewItemParametersConverter" />
            </Button.Resources>
            <Button.CommandParameter>
                <MultiBinding Converter="{StaticResource RemoveListViewItemParametersConverter}">
                <MultiBinding.Bindings>
                    <Binding Path="AgencyID" />
                    <Binding Path="ID" />
                </MultiBinding.Bindings>
                </MultiBinding>
            </Button.CommandParameter>
            </Button>
        </DataTemplate>
        </GridViewColumn.CellTemplate>                                                   
    </GridViewColumn>
    </GridView>                                            
</ListView.View>

1 个答案:

答案 0 :(得分:1)

我在ListView中遇到了类似的问题。

基本上,ListView的每个项目都有一个RadioButton和一个TextBox。 RadioButton IsChecked属性绑定到ListViewItem选择属性。问题是,当我选择TextBox时,未选择该项,因此不检查RadioButton。

我设法用IsKeyboardFocusWithin属性解决了这个问题。我在ListViewItem样式中设置了一个触发器,因此当此属性为true时,isSelected属性也将设置为true。

您可以查看thread