简体   繁体   English

ListView DataTemplate WPF中项目之间的距离

[英]distance between items in listview datatemplate wpf

hi i created a datatemplate for listview now i have 2 problem 嗨,我现在为listview创建了一个datatemplate我有2个问题
first is when mouse focus on button my image hide you can see it on below image. 首先是当鼠标聚焦在我的图像按钮上时,您可以在下面的图像上看到它。
problem 1 and 2 two is The distance between the items is tall and I want their distance being just one line (see below image) see this image 问题1和2 两个是项目之间的距离很高,我希望它们之间的距离只有一条线(请参见下图) 请参见此图

and this is my datatemplate: 这是我的数据模板:

 <DataTemplate>
    <Border  Background="#f0f4f7">
        <StackPanel Background="#f5f6fa" Margin="1,1,1,1" VerticalAlignment="Top">
            <Border Background="#edf0f5" BorderThickness="5">
                <Grid  Background="#ffffff" Height="30">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>
                    <StackPanel Background="#ffffff" Margin="5" Orientation="Horizontal">
                        <Button Height="20" Width="20" BorderBrush="Transparent" BorderThickness="0" Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.DeleteCommand}">
                            <Button.Background>
                                <ImageBrush ImageSource="..\Resources\Delete.png"/>
                            </Button.Background>
                        </Button>
                        <Button Height="20" Width="20" BorderBrush="Transparent" BorderThickness="0" Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.EditCommand}">
                            <Button.Background>
                                <ImageBrush ImageSource="..\Resources\Edit.png"/>
                            </Button.Background>
                        </Button>
                    </StackPanel>
                    <TextBlock Name="txtPhon" Foreground="#7c7f84" HorizontalAlignment="Right" Grid.Column="1" Text="{Binding Path=HomePhoneNumber}"
    Margin="0,5,5,5"/>
                </Grid>
            </Border>
        </StackPanel>


    </Border>
</DataTemplate>

The problem with mouse making the picture disappear is because of the default template to a button. 鼠标使图片消失的问题是由于按钮的默认模板。 When you mouseover it gives that blue effect. 当您将鼠标悬停在上面时,会产生蓝色效果。 I think it's in a border which is inside the template so it's above the background. 我认为它位于模板内部的边框中,因此位于背景上方。 You can re-template the button to avoid that if you just want your image inside it. 您可以重新模板化按钮,以避免仅在其中包含图像的情况。 Depending on your exact requirements then you might want slightly different but roughly: 根据您的确切要求,您可能希望略有不同,但大致不同:

    <Button.Template>
        <ControlTemplate>
            <Border>
               <ContentPresenter />
            </Border>
        </ControlTemplate>
    </Button.Template>

You could make the whole template just an image. 您可以使整个模板只是一个图像。 Particularly if your icons are one colour, I would recommend using a path and define your icons as geometries in a resource dictionary. 特别是如果您的图标是一种颜色,我建议您使用路径并在资源字典中将图标定义为几何。 Like the letter in this: https://social.technet.microsoft.com/wiki/contents/articles/32610.wpf-layout-lab.aspx 像这样的信: https : //social.technet.microsoft.com/wiki/contents/articles/32610.wpf-layout-lab.aspx

The gap between your items. 您的商品之间的差距。 There's padding in the default itemcontainer styling. 默认的itemcontainer样式中有填充。 You can avoid that by, again, changing the template something like: 您可以通过再次更改模板来避免这种情况,例如:

    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListViewItem}">
                        <ContentPresenter/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListView.ItemContainerStyle>

I notice you also have a margin set on your TextBlock, I would temporarily change that and see if that's also part of the problem. 我注意到您在TextBlock上也设置了一个边距,我会暂时更改它,看看这是否也是问题的一部分。

               <TextBlock Name="txtPhon" Foreground="#7c7f84" HorizontalAlignment="Right" Grid.Column="1" Text="{Binding Path=HomePhoneNumber}"
**Margin="0,0,5,0"/>**

In order to explore this sort of issue I find Snoop is invaluable. 为了探究此类问题,我发现Snoop是无价的。 It's free. 免费。 Once installed run it after your app. 安装完成后,在您的应用程序之后运行它。 Drag one of the sight + things over your running window. 将视线+事物之一拖到正在运行的窗口上。 Mouse over a suspect piece of UI. 将鼠标悬停在可疑的用户界面上。 Press Ctrl+Shift and it'll show you all the controls in there and their properties. 按Ctrl + Shift,它将向您显示其中的所有控件及其属性。 You can change values in the window and explore what effect the change would have immediately. 您可以在窗口中更改值,并探索更改将立即产生的影响。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM