Search code examples
c#wpfxamlstylesalternation

Appearance of Textbox within a view column shall be identical to the appearance of list rows


My ListView contains a textbox per column to change data. Via Style my Listview rows are colored in different colors depending on the alternation index.

I want my textboxes within the listview to look like the entire line.

Actually its like this: enter image description here

The lines should cover the textboxes.

The Code to color the ListView Rows:

         <Style x:Key="DifAlternationColorsLV" TargetType="{x:Type ListViewItem}">
            <Style.Triggers>
                <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                    <Setter Property="Background" Value="#FFB1E4EF"></Setter>
                </Trigger>
                <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                    <Setter Property="Background" Value="#FFD4EBFF"></Setter>
                </Trigger>
            </Style.Triggers>
        </Style>

The code in the XAML of the View:

    <Border Grid.Row="1" Grid.Column="0" 
            Margin="10,10,10,10"
            BorderBrush="#FF474A57" 
            CornerRadius="10,10,10,10" 
            BorderThickness="2,2,2,2"
            Width="300"
            MaxHeight="200"
            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">
                    Schichten
                </Label>
                <ListView x:Name="Shift" MinHeight="150" MaxHeight="150" MinWidth="250" HorizontalContentAlignment="Stretch" HorizontalAlignment="Center"
                      IsSynchronizedWithCurrentItem="True"
                          ItemContainerStyle="{StaticResource DifAlternationColorsLV}"
                          AlternationCount="2"
                          ItemsSource="{Binding UiShiftHModelList, UpdateSourceTrigger=PropertyChanged}"
                          SelectedItem="{Binding SelectedLine, UpdateSourceTrigger=PropertyChanged}" d:ItemsSource="{d:SampleData ItemCount=3}">
                    <ListView.View>
                        <GridView>
                            <GridView.Columns>
                                <GridViewColumn Header="Bezeichnung" Width="Auto">
                                    <GridViewColumn.CellTemplate>
                                        <DataTemplate>
                                            <TextBox x:Name="Bezeichnung" MinWidth="100"
                                                     
                                                     Style="{StaticResource TBoxInListV}"
                                                     Text="{Binding Bezeichnung, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                                                     BorderThickness="0">
                                            </TextBox>
                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                </GridViewColumn>

                                <GridViewColumn Header="Tagmuster" Width="80">
                                    <GridViewColumn.CellTemplate>
                                        <DataTemplate>
                                            <!---->
                                            <TextBox x:Name="Tagmuster" MinWidth="80"
                                                     Background="{Binding ElementName=Shift}"
                                                     Style="{StaticResource TBoxInListV}"
                                                     Text="{Binding TagmusterID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                                                      
                                                     BorderThickness="0">
                                            </TextBox>
                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                </GridViewColumn>

                                <GridViewColumn Header="Edit" Width="40">
                                    <GridViewColumn.CellTemplate>
                                        <DataTemplate>
                                            <StackPanel Orientation="Horizontal" >
                                                <Grid>
                                                    <Grid.ColumnDefinitions>
                                                        <ColumnDefinition Width="*" />
                                                        <ColumnDefinition Width="Auto" />
                                                        <ColumnDefinition Width="*" />
                                                    </Grid.ColumnDefinitions>
                                                    <Button  x:Name="SaveShiftH" 
                                                             Style="{StaticResource BtnListSave}"
                                                             Grid.Column="0">
                                                    </Button>
                                                    <Button  x:Name="UpdateShiftH" 
                                                              Style="{StaticResource BtnListUpdate}"
                                                             Grid.Column="0">
                                                    </Button>
                                                    <Button x:Name="DeleteShiftH" 
                                                             Style="{StaticResource BtnListDelete}"
                                                             Grid.Column="1">
                                                    </Button>
                                                    <Button x:Name="ReopenShiftH"  
                                                            Style="{StaticResource BtnListReopen}"
                                                             Grid.Column="0">
                                                    </Button>
                                                </Grid>
                                            </StackPanel>
                                            <DataTemplate.Triggers>
                                                <!--Visibility of the Buttons-->
                                                <DataTrigger Binding="{Binding EditModus}" Value="0">
                                                    <Setter TargetName="SaveShiftH" Property="Visibility" Value="Visible"/>
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding EditModus}" Value="2">
                                                    <Setter TargetName="UpdateShiftH" Property="Visibility" Value="Visible"/>
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding EditModus}" Value="1">
                                                    <Setter TargetName="UpdateShiftH" Property="Visibility" Value="Collapsed"/>
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding EditModus}" Value="2">
                                                    <Setter TargetName="DeleteShiftH" Property="Visibility" Value="Visible"/>
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding EditModus}" Value="1">
                                                    <Setter TargetName="DeleteShiftH" Property="Visibility" Value="Collapsed"/>
                                                </DataTrigger>
                                            <!--<DataTrigger Binding="{Binding EditModus}" Value="0">
                                                    <Setter TargetName="Delete" Property="Visibility" Value="Collapsed"/>
                                                </DataTrigger>-->
                                                <DataTrigger Binding="{Binding EditModus}" Value="1">
                                                    <Setter TargetName="ReopenShiftH" Property="Visibility" Value="Visible"/>
                                                </DataTrigger>
                                            </DataTemplate.Triggers>
                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                </GridViewColumn>
                            </GridView.Columns>
                        </GridView>
                    </ListView.View>
                </ListView>
            </StackPanel>
        </StackPanel>
    </Border>

If I change the Background Binding from the Textbox to:

Background="{Binding ElementName=Shift}"

The Line appears exactly I want it to but leads to a lot binding errors. enter image description here

Any ideas?


Solution

  • You can make the background of the TextBox wholly or partially transparent; that will make the background of the row visible:

        <TextBox x:Name="Bezeichnung" MinWidth="100" Background="#60FFFFFF"
            Style="{StaticResource TBoxInListV}"
            Text="{Binding Bezeichnung, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
            BorderThickness="0">
        </TextBox>