none
GridView的绑定只显示实体的toString()方法的内容,所有的GridViewColumn的设置都不显示 RRS feed

  • 问题

  • 设置ListViewd展示方式为GridView,但无论GridView里怎么设置模板,除了能显示header的内容,集合数据的绑定显示始终都是实体ToString()的内容。但把<ListView.view>换成自定义的DataTemplate就能正常显示内容,ItemsPanelTemplate的设置也有效果。怎么解决GridView绑定只显示toString()方法的内容问题?

    我查出来了,好像是与我设计的ListViewItem的全局默认style有关。style样式如下,我想设计一个全局通用默认的ListViewItem的style

     <Style  TargetType="{x:Type ListViewItem}">
    
    
            <Setter Property="SnapsToDevicePixels" Value="True" />
            <Setter Property="IsSelected" Value="{Binding IsSelected}"></Setter>
            <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
            <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
              <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListViewItem}">
                        <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
                            <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsMouseOver" Value="True"/>
                                </MultiTrigger.Conditions>
                                <Setter Property="Background" TargetName="Bd" Value="red"/>
                                <!--<Setter Property="BorderBrush" TargetName="Bd" Value="blue"/>-->
                            </MultiTrigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="Selector.IsSelectionActive" Value="False"/>
                                    <Condition Property="IsSelected" Value="True"/>
                                </MultiTrigger.Conditions>
                                <Setter Property="Background" TargetName="Bd" Value="blue"/>
                                <!--<Setter Property="BorderBrush" TargetName="Bd" Value="green"/>-->
                            </MultiTrigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="Selector.IsSelectionActive" Value="True"/>
                                    <Condition Property="IsSelected" Value="True"/>
                                </MultiTrigger.Conditions>
                                <Setter Property="Background" TargetName="Bd" Value="green"/>
                                <!--<Setter Property="BorderBrush" TargetName="Bd" Value="pink "/>-->
                            </MultiTrigger>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter Property="TextElement.Foreground" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    

    • 已编辑 Trian555 2019年8月8日 1:52
    2019年8月6日 6:40

答案

  •   
    Hi    Trian555,
      
    当你以GridView模式显示ListView 的时候,你需要用GridViewRowPresenter  

        <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
                                <GridViewRowPresenter Content="{TemplateBinding Content}"
           Columns="{TemplateBinding GridView.ColumnCollection}"/>
                                <!--<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>-->
                            </Border>


    Best regards

    Yong Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 Trian555 2019年8月8日 8:03
    2019年8月8日 6:34
    版主

全部回复

  • 方便贴一下代码吗?是Winform程序吗

    ericzhou

    2019年8月6日 8:38
  • 设置ListViewd展示方式为GridView,但无论GridView里怎么设置模板,除了能显示header的内容,集合数据的绑定显示始终都是实体ToString()的内容。但把<ListView.view>换成自定义的DataTemplate就能正常显示内容,ItemsPanelTemplate的设置也有效果。怎么解决GridView绑定只显示toString()方法的内容问题?

    Hi  Trian555,

    下面的小例子可以正常显示ListView的GridView视图模式。

        <ListView Margin="10" Name="lvUsers">
    			<ListView.View>
    				<GridView>
    					<GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding Name}" />
    					<GridViewColumn Header="Age" Width="50" DisplayMemberBinding="{Binding Age}" />
    					<GridViewColumn Header="Mail" Width="150" DisplayMemberBinding="{Binding Mail}" />
    				</GridView>
    			</ListView.View>
    		</ListView>
    
      public partial class ListViewGridViewSample : Window
    	{
    		public ListViewGridViewSample()
    		{
    			InitializeComponent();
    			List<User> items = new List<User>();
    			items.Add(new User() { Name = "John Doe", Age = 42, Mail = "john@doe-family.com" });
    			items.Add(new User() { Name = "Jane Doe", Age = 39, Mail = "jane@doe-family.com" });
    			items.Add(new User() { Name = "Sammy Doe", Age = 7, Mail = "sammy.doe@gmail.com" });
    			lvUsers.ItemsSource = items;
    		}
    	}
    
    	public class User
    	{
    		public string Name { get; set; }
    
    		public int Age { get; set; }
    
    		public string Mail { get; set; }
    	}
    Best regards

    Yong Lu


     

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2019年8月7日 3:17
    版主
  • 设置ListViewd展示方式为GridView,但无论GridView里怎么设置模板,除了能显示header的内容,集合数据的绑定显示始终都是实体ToString()的内容。但把<ListView.view>换成自定义的DataTemplate就能正常显示内容,ItemsPanelTemplate的设置也有效果。怎么解决GridView绑定只显示toString()方法的内容问题?

    Hi  Trian555,

    下面的小例子可以正常显示ListView的GridView视图模式。

        <ListView Margin="10" Name="lvUsers">
    			<ListView.View>
    				<GridView>
    					<GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding Name}" />
    					<GridViewColumn Header="Age" Width="50" DisplayMemberBinding="{Binding Age}" />
    					<GridViewColumn Header="Mail" Width="150" DisplayMemberBinding="{Binding Mail}" />
    				</GridView>
    			</ListView.View>
    		</ListView>
      public partial class ListViewGridViewSample : Window
    	{
    		public ListViewGridViewSample()
    		{
    			InitializeComponent();
    			List<User> items = new List<User>();
    			items.Add(new User() { Name = "John Doe", Age = 42, Mail = "john@doe-family.com" });
    			items.Add(new User() { Name = "Jane Doe", Age = 39, Mail = "jane@doe-family.com" });
    			items.Add(new User() { Name = "Sammy Doe", Age = 7, Mail = "sammy.doe@gmail.com" });
    			lvUsers.ItemsSource = items;
    		}
    	}
    
    	public class User
    	{
    		public string Name { get; set; }
    
    		public int Age { get; set; }
    
    		public string Mail { get; set; }
    	}
    Best regards

    Yong Lu


     

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    我查出来了,是我设计的ListViewItem的style有问题,你能看看有哪些问题吗?style代码在上面
    2019年8月8日 1:54
  •   
    Hi    Trian555,
      
    当你以GridView模式显示ListView 的时候,你需要用GridViewRowPresenter  

        <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
                                <GridViewRowPresenter Content="{TemplateBinding Content}"
           Columns="{TemplateBinding GridView.ColumnCollection}"/>
                                <!--<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>-->
                            </Border>


    Best regards

    Yong Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 Trian555 2019年8月8日 8:03
    2019年8月8日 6:34
    版主