当前ListView行的WPF绑定属性

时间:2019-02-12 12:12:56

标签: c# wpf listview

我正在尝试在TreeView内实现ListView

我希望TreeBox的背景与其“父亲”行相同。 我猜有可能将其BackGround属性绑定到其行的BackGround属性,但是该怎么做?

下面是一张说明问题的图片:

选择第一行 第二行结束了

TreeViewInListView

我还放了XAML,以防万一:

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

编辑:

最后,放入“ transparent并不能完全解决问题,有时可以,但是有时在选择Treeview时似乎被选中了,而我得到以下结果:

enter image description here

然后当我取消选择它时,它会给出结果:

enter image description here

我希望Treeview可以作为行中的任何元素,为此,我发现的唯一方法是在每个TextBlock上添加一个MouseDown事件,然后执行此操作:

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

不知道是否有更简单的方法来做到这一点,但这就是我到目前为止找到的所有内容。

2 个答案:

答案 0 :(得分:1)

尝试将Background的{​​{1}}属性设置为TreeView

Transparent

答案 1 :(得分:0)

我把它放在这里是给谁来的:

how to change highlight values

这里很好地解释了什么是SystemColors参数,最后只需添加:

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