开发者

How to select ListView row if control in data template is clicked

开发者 https://www.devze.com 2023-02-16 07:27 出处:网络
I have a ListView that is using a DataTemplate.I swap out the Item DataTemplate based on the IsSelected property of the particular item.This allows me to display an edit template and a read template.T

I have a ListView that is using a DataTemplate. I swap out the Item DataTemplate based on the IsSelected property of the particular item. This allows me to display an edit template and a read template. The ListView contains two columns. In read mode the two columns are readonly TextBoxes and in edit mode the left column is a editable TextBox and the right column is a drop down. Everything works great as long as I don't click directly on one of the TextBoxes when in read mode. If I click outside the control the row is selected just fine, when selecting inside the control, however, the row is not selected. This prevents me from entering edit mode when one of the cells are clicked.

I've pasted my xaml below. You can see that the GridBlock and GridEdit styles will be controlled by the IsSelected property of the ListView. This is what allows me to swap out the DataTemplate (really hide or collapse) based on that DP. I've further specialized these styles to allow for watermarked textboxes when the value is empty and the control doesn't have focus. I think this is where my problem lies but for the life of me I can't think of a way to do this declaratively. Also, I'm new to WPF so I'm sure there is a pattern for this sort of thing it's just very difficult to formulate a query that will return meaningful results from google or bing. Thanks for any and all help in advance.

Here are my styles and datatemplates:

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

And here is my ListView view:

<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"  Mar开发者_JAVA百科gin="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>


I had a similar problem in a ListView.

Basically each item of the ListView had a RadioButton and a TextBox. The RadioButton IsChecked property was binded to the ListViewItem select property. The thing was that when I selected the TextBox the item was not selected, hence not checking the RadioButton.

I managed to solve the problem with the IsKeyboardFocusWithin property. I set a trigger in the ListViewItem style so when this property is true the isSelected property would be set to true also.

You can check this thread.

0

精彩评论

暂无评论...
验证码 换一张
取 消