Siegfried.V
Siegfried.V

Reputation: 1595

WPF bind property of current ListView row

I am trying to implement a TreeView inside my ListView.

I would like the backgroud of my TreeBox would be the same as its "Father" row. I guess there is a possibility to bind its BackGround property to its row's BackGround property, but how to do that?

Here is an image to explain the issue :

1st line is selected 2nd line is Over

TreeViewInListView

I also put my XAML, in case that would help :

<ListView x:Name="ListView4" SelectedItem="{Binding SelectedRepere}" ItemsSource="{Binding ListeDesReperes}"  Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="4" MouseDoubleClick="ListView_MouseDoubleClick" GridViewColumnHeader.Click="GridViewColumnHeaderClickedHandler" ContextMenuOpening="ListView4_ContextMenuOpening" SelectionChanged="ListView4_SelectionChanged" Visibility="{Binding Grid4Visible, Converter={StaticResource BoolToVisConverter}}" >
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=IsUniqueGeometry}" Value="true">
                    <Setter Property="FontWeight" Value="Bold" />
                </DataTrigger>
            </Style.Triggers>

        </Style> 
    </ListView.ItemContainerStyle>
    <ListView.ContextMenu>
        <ContextMenu x:Name="Context4">
            <MenuItem x:Name="Context4MakeLonger" Header="{x:Static p:Resources.MakeLonger}" Click="Make_Longer"/>
            <MenuItem x:Name="Context4Distinguer" Header="{x:Static p:Resources.DistributeToAss}" Click="DistinguerDetailRepere"/>
            <MenuItem x:Name="Context4Search" Header="{x:Static p:Resources.Search}" Click="Search_Detail"/>
        </ContextMenu>
    </ListView.ContextMenu>
    <ListView.View>

        <GridView AllowsColumnReorder="true" x:Name="GridView4">
            <GridViewColumn DisplayMemberBinding="{Binding Path=ID}" Header="ID" Width="200"/>
            <GridViewColumn Header="Name">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <TreeView BorderThickness="0" x:Name="treeviewList" ItemsSource="{Binding RepereTree}" Width="Auto">
                                <TreeView.ItemContainerStyle>
                                    <Style TargetType="{x:Type TreeViewItem}">
                                        <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
                                        <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}"/>
                                        <Setter Property="HorizontalAlignment" Value="Left"/>
                                    </Style>
                                </TreeView.ItemContainerStyle>
                                <TreeView.ItemTemplate>
                                    <DataTemplate>
                                        <TreeViewItem  ItemsSource="{Binding ListeSubReperes}">
                                            <TreeViewItem.Header>
                                                <Grid>
                                                    <TextBlock Foreground="#FF042271" Text="{Binding NameOri}" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Column="0" Tag="{Binding Name}" MouseMove="mouseOverNameRepere" ToolTip="{Binding Path=ToolTipModifications}" MouseDown="TreeView_Main_Click"/>
                                                </Grid>
                                            </TreeViewItem.Header>
                                            <TreeViewItem.ItemTemplate>
                                                <DataTemplate>
                                                    <Grid Margin="-20,0,0,0">
                                                        <Grid.ColumnDefinitions>
                                                            <ColumnDefinition Width="Auto"></ColumnDefinition>
                                                            <ColumnDefinition Width="Auto"></ColumnDefinition>
                                                        </Grid.ColumnDefinitions>
                                                        <TextBlock Foreground="#FF042271" Text="{Binding Name}" Tag="{Binding IdRepereOri}" Margin="10,0,0,0" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Column="0"MouseDown="TreeView_Sub_Click"/>

                                                    </Grid>
                                                </DataTemplate>
                                            </TreeViewItem.ItemTemplate>
                                        </TreeViewItem>
                                    </DataTemplate>
                                </TreeView.ItemTemplate>
                            </TreeView>
                        </StackPanel>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            ...

Edit :

Finally, putting "transparent doesn't completely solve the proble, sometimes it works, but sometimes when selecting the Treeview, it seems it gets selected, and I have the following result :

enter image description here

Then when I unselect it, it gives that result :

enter image description here

I would like the Treeview would be as any element of my row, for this the only way I found is add on each TextBlock a MouseDown event, then I do this :

private void TreeView_Main_Click(object sender, MouseButtonEventArgs e)
{
    TextBlock item = (TextBlock)sender;
    string name = (string)item.Text;
    Repere rep = contexte.ListeDesReperes.FirstOrDefault(x => x.Name == name);
    if (rep != null)
    {
        contexte.SelectedRepere = rep;
    }
}
private void TreeView_Sub_Click(object sender, MouseButtonEventArgs e)
{
    TextBlock item = (TextBlock)sender;
    long idOri = (long)item.Tag;
    Repere rep = contexte.ListeDesReperes.FirstOrDefault(x => x.ID==idOri);
    if (rep != null)
    {
        contexte.SelectedRepere = rep;
    }
}

Don't know if there is an easier way to do that, but that's all what I found until now.

Upvotes: 0

Views: 394

Answers (2)

Siegfried.V
Siegfried.V

Reputation: 1595

I put it here for who would come here :

how to change highlight values

Here it is well explained what are SystemColors parameters, on the end just had to add :

<TreeView.Resources>
   <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
   <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey }" Color="Transparent" />
   <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
</TreeView.Resources>

Upvotes: 0

mm8
mm8

Reputation: 169150

Try to set the Background property of the TreeView to Transparent:

<TreeView Background="Transparent">

Upvotes: 1

Related Questions