WPF Column in ListView shall represent a ComboBox when IsFocused = true but a simple TextBox when IsFocused = false

When the cell in the column is in focus the ComboBox shall appear but once the value is selected and the cell is not in focus anymore, only the text shall appear. So the ComboBox shall only be visible when cell is in focus.

This is my code but I’ve really no clue how to solve that.

                   <ListView.View>
                        <GridView>
                            <GridView.Columns>
                                
                                <GridViewColumn Header="SchichtID" Width="60">
                                    <GridViewColumn.CellTemplate>
                                        <DataTemplate>
                                            <ComboBox x:Name="SelectedShiftHID"
                                                SelectedIndex="{Binding SchichtID}"
                                                DisplayMemberPath="Bezeichnung"
                                                ItemsSource="{Binding DataContext.UiShiftHModelList, Mode=OneWay,RelativeSource={RelativeSource AncestorType=ListView},UpdateSourceTrigger=PropertyChanged}"/>
                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                </GridViewColumn>

                            </GridView.Columns>
                        </GridView>
                    </ListView.View>

1.EDIT:
What I’m trying here is to put combobox into a column of a ListView. The values ​​published there come from Model A. The DisplayedMemberPath is the description of the row from model a. We save the ID of that row from Model A in Model B. When the data is reloaded the correct description shall be loaded and shown again in the way explained in my initial post.

2.EDIT:
@Anton – your answer doesn’t work. It starts that there is no comboBox shown when focusingsing the cell neither it shows any text.

In the XAML of the View im introducing the converters:

<UserControl.Resources>
        <helpers:LastRowVisibilityMultiValueConverter x:Key="LastRowVisibilityMultiValueConverter" />

        <helpers:ShiftHIDtoDescriptionConverter x:Key="ShiftHIDtoDescriptionConverter" ShiftH="{Binding DataContext.UiShiftHModelList, Mode=OneWay, ElementName=ShiftT, UpdateSourceTrigger=PropertyChanged}"/>

        <helpers:CellTemplateSelector x:Key="cellTemplateSelector" x:Name="cellTemplateSelector">
            <helpers:CellTemplateSelector.EditableTemplate>
                <DataTemplate>
                    <ComboBox x:Name="SelectedShiftHID"
                              SelectedIndex="{Binding ID}"
                              DisplayMemberPath="Bezeichnung"
                              ItemsSource="{Binding UiShiftHModelList, Mode=OneWay,ElementName=ShiftT,UpdateSourceTrigger=PropertyChanged}"/>
                </DataTemplate>
            </helpers:CellTemplateSelector.EditableTemplate>

            <helpers:CellTemplateSelector.ReadOnlyTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding SchichtID, Converter={StaticResource ShiftHIDtoDescriptionConverter}}"/>
                </DataTemplate>
            </helpers:CellTemplateSelector.ReadOnlyTemplate>
        </helpers:CellTemplateSelector>
        
    </UserControl.Resources>

There simply happens nada.

One error I’ve got in your suggested converter was:

public static readonly DependencyProperty ItemsProperty =     DependencyProperty.Register("Items", typeof(IEnumerable), typeof(ItemIdToStringConverter:DependencyObject), new PropertyMetadata(null));

This here: typeof(ItemIdToStringConverter:DependencyObject)

Following the adjusted converter:

public class ShiftHIDtoDescriptionConverter : DependencyObject, IValueConverter
{
    public static readonly DependencyProperty ShiftHProperty = DependencyProperty.Register("ShiftH", typeof(IEnumerable), typeof(ShiftHIDtoDescriptionConverter),new PropertyMetadata(null));

    public IEnumerable ShiftH
    {
        get { return (IEnumerable)GetValue(ShiftHProperty); }
        set { SetValue(ShiftHProperty, value); }
    }

    public object Convert(object shiftHID, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
        int? id = shiftHID as int?;
        if (id != null) {
            return ShiftH.Cast<UiShiftHModel>().FirstOrDefault(m => m.ID == id)?.Bezeichnung;
        }
        return null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
        throw new NotImplementedException();
    }
}

This here is the XAML part:

<Border Grid.Row="1" Grid.Column="1" 
            Margin="10,10,10,10"
            BorderBrush="#FF474A57" 
            CornerRadius="10,10,10,10" 
            BorderThickness="2,2,2,2"
            Width="520"
            MaxHeight="300"
            Background="White">
        <StackPanel Margin="0,0,0,20" Orientation="Vertical">
            <StackPanel Grid.Column="0" Grid.RowSpan="1"
                Grid.Row="1"
                VerticalAlignment="Top">
                <Label HorizontalAlignment="Center" FontWeight="Bold">
                    Schichtdetails
                </Label>

                <ListView x:Name="ShiftT" MinHeight="150" MaxHeight="200" MinWidth="500" HorizontalContentAlignment="Stretch" HorizontalAlignment="Center"
                          ItemContainerStyle="{DynamicResource DifAlternationColorsLV}"
                          AlternationCount="2"
                          ItemsSource="{Binding UiShiftTModelList, UpdateSourceTrigger=PropertyChanged}" d:ItemsSource="{d:SampleData ItemCount=5}">
                    <ListView.View>
                        
                        <GridView>
                          
                            <GridView.Columns>

                                <GridViewColumn Header="ID" Width="30">
                                    <GridViewColumn.CellTemplate>
                                        <DataTemplate>
                                            <TextBox x:Name="ID" MinWidth="30"
               
                                                     Style="{StaticResource TBoxInListV}"
                                                     Text="{Binding ID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                                                      
                                                     BorderThickness="0">
                                            </TextBox>
                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                </GridViewColumn>


                                <GridViewColumn Header="SchichtID" Width="60">
                                    <GridViewColumn.CellTemplate>
                                        <DataTemplate>
                                            <ContentControl ContentTemplateSelector="{StaticResource cellTemplateSelector}"/>
                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                </GridViewColumn>

                            </GridViewColumn>
                            </GridView.Columns>
                        </GridView>
                    </ListView.View>
                </ListView>
            </StackPanel>
        </StackPanel>
    </Border>

Leave a Comment