Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4049440
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T13:56:44+00:00 2026-05-20T13:56:44+00:00

I have a ListView that is using a DataTemplate. I swap out the Item

  • 0

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"  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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T13:56:44+00:00Added an answer on May 20, 2026 at 1:56 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ListView that I need to update by adding an item. Using
I have a ListView that is populated using an XML file. However, I want
I have a list of items that are displayed using a ListView from a
I have ListView that uses a GridView to display several columns of data. Two
I have a listview that generates thumbnail using a backgroundworker. When the listview is
I have a ListView subclass that display rows that each contain an EditText and
I have a situation about in using ListView and found that if I set
I have a listview that uses a customadapter based on the baseadapter. The listview
I have a simple ListView that gets populated using an array of SimpleAdapter s.
I have create android listview that fill in using String array. I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.