如果我修复了所有ListViewItems的HeightProperty,如何设置ListView中ListViewItems上的文本换行样式?

时间:2013-07-11 15:25:19

标签: c# wpf listview styling listviewitem

这是我的代码(您可以在VS中创建一个新的WPF项目并运行它,只需将MainWindow的XAML中的Grid名称设置为MainGrid):

namespace WPFTesting
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        ObservableCollection<Message> messages = new ObservableCollection<Message>();

        public MainWindow()
        {
            InitializeComponent();
            messages.Add(new Message(DateTime.Now, "This is a test."));
            messages.Add(new Message(DateTime.Now, "This is a multi-line message.\nThis is a multi-line message."));
            messages.Add(new Message(DateTime.Now, "This is a multi-line message.\nThis is a multi-line message."));
            messages.Add(new Message(DateTime.Now, "This is a multi-line message.\nThis is a multi-line message.")); 
            ListView listView = new ListView();
            Style style = new Style();
            style.TargetType = typeof(ListViewItem);
            DataTrigger trigger = new DataTrigger();
            trigger.Binding = new Binding("Text");
            trigger.Value = "This is a test.";
            trigger.Setters.Add(new Setter(ListViewItem.BackgroundProperty, Brushes.Pink));
            style.Triggers.Add(trigger);
            style.Setters.Add(new Setter(ListViewItem.HeightProperty, 20.0));
            style.Setters.Add(new Setter(ListViewItem.MarginProperty, new Thickness(0)));
            style.Setters.Add(new Setter(ListViewItem.BorderThicknessProperty, new Thickness(0)));
            listView.ItemContainerStyle = style;
            GridView gridView = new GridView();
            listView.View = gridView;
            GridViewColumn timeStampColumn = new GridViewColumn();
            timeStampColumn.DisplayMemberBinding = new Binding("Date");
            GridViewColumnHeader timeStampHeader = new GridViewColumnHeader();
            timeStampHeader.Content = "Time";
            timeStampColumn.Header = timeStampHeader;
            gridView.Columns.Add(timeStampColumn);
            GridViewColumn messageColumn = new GridViewColumn();
            messageColumn.DisplayMemberBinding = new Binding("Text");
            GridViewColumnHeader messageHeader = new GridViewColumnHeader();
            messageHeader.Content = "Message";
            messageColumn.Header = messageHeader;
            gridView.Columns.Add(messageColumn);
            Binding binding = new Binding();
            binding.Source = messages;
            listView.SetBinding(ItemsControl.ItemsSourceProperty, binding);
            MainGrid.Children.Add(listView);
        }

        public class Message
        {

            public Message(DateTime aDate, String aText)
            {
                Date = aDate;
                Text = aText;
            }

            public DateTime Date { get; set; }
            public String Text { get; set; }
        }
    }
}

正如您所看到的,由于我将每个ListViewItem的高度限制为20.0,因此多行消息在ListView中显示出令人讨厌的类型。我真的只想要显示文本消息的第一行。我怎样才能实现这种行为?

2 个答案:

答案 0 :(得分:1)

以下是您的问题的一种解决方案。为格式化而烦恼...我仍然无法获得SOverFlow的格式化以便为我工作。

  1. 我已经完成了从XAML的代码隐藏文件中删除了所有创建代码。我将Messages的定义移到了它自己的ViewModel类(MSGviewModel.cs)

  2. 在主窗口的XAML中,我已经定义了ListView并设置了一个简单的ItemTemplate。此ItemTemplate将显示日期和消息文本。显示消息文本的TextBLock限制为最大高度为20。

  3. 我为该TextBlock定义了一个工具提示。该工具提示绑定到Message的完整Text属性。这样你就可以将鼠标单行消息并显示全文。

  4. MAINWINDOW XAML:

    <Window x:Class="FixListView.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <Grid Name="MainGrid">
            <ListView ItemsSource="{Binding Messages}">
              <ListView.ItemTemplate>        
                <DataTemplate>          
                  <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Text}" MaxHeight="20">
                      <TextBlock.ToolTip>
                        <TextBlock Text="{Binding Text}"/>
                      </TextBlock.ToolTip>
                    </TextBlock>
                    <TextBlock Text=" -- "/>
                    <TextBlock Text="{Binding Date}"/>
                  </StackPanel>
                </DataTemplate>
              </ListView.ItemTemplate>
            </ListView>
          </Grid>
        </Window>
    

    MAINWINDOW - 代码背后:

    public partial class MainWindow : Window
    {
      public MainWindow()
      {
        this.DataContext = new MSGviewModel();
        InitializeComponent();
      }
    }
    

    VIEWMODEL课程:

    public class MSGviewModel
    {
    private ObservableCollection<Message> messages = new ObservableCollection<Message>();
    public ObservableCollection<Message> Messages
    {
      get { return messages; }
    }
    
    
    public MSGviewModel()
    {
      messages.Add(new Message(DateTime.Now, "This is a test."));
      messages.Add(new Message(DateTime.Now, "This is a multi-line message.\nThis is a multi-line message."));
      messages.Add(new Message(DateTime.Now, "This is a multi-line message.\nThis is a multi-line message."));
      messages.Add(new Message(DateTime.Now, "This is a multi-line message.\nThis is a multi-line message.")); 
    }
    }
    
    public class Message
    {
    
      public Message(DateTime aDate, String aText)
      {
        Date = aDate;
        Text = aText;
      }
    
      public DateTime Date { get; set; }
      public String Text { get; set; }
    }
    }
    

答案 1 :(得分:0)

这是在维护当前代码时执行此操作的一种方法:

namespace WPFTesting
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        ObservableCollection<Message> messages = new ObservableCollection<Message>();

        public MainWindow()
        {
            InitializeComponent();
            messages.Add(new Message(DateTime.Now, "This is a test."));
            messages.Add(new Message(DateTime.Now, "This is a multi-line message.\nThis is a multi-line message."));
            messages.Add(new Message(DateTime.Now, "This is a multi-line message.\nThis is a multi-line message."));
            messages.Add(new Message(DateTime.Now, "This is a multi-line message.\nThis is a multi-line message.")); 
            ListView listView = new ListView();
            Style style = new Style();
            style.TargetType = typeof(ListViewItem);
            DataTrigger trigger = new DataTrigger();
            trigger.Binding = new Binding("Text");
            trigger.Value = "This is a test.";
            trigger.Setters.Add(new Setter(ListViewItem.BackgroundProperty, Brushes.Pink));
            style.Triggers.Add(trigger);
            style.Setters.Add(new Setter(ListViewItem.HeightProperty, 20.0));
            style.Setters.Add(new Setter(ListViewItem.MarginProperty, new Thickness(0)));
            style.Setters.Add(new Setter(ListViewItem.BorderThicknessProperty, new Thickness(0)));
            listView.ItemContainerStyle = style;
            GridView gridView = new GridView();
            listView.View = gridView;
            GridViewColumn timeStampColumn = new GridViewColumn();
            timeStampColumn.DisplayMemberBinding = new Binding("Date");
            GridViewColumnHeader timeStampHeader = new GridViewColumnHeader();
            timeStampHeader.Content = "Time";
            timeStampColumn.Header = timeStampHeader;
            gridView.Columns.Add(timeStampColumn);
            GridViewColumn messageColumn = CreateGridViewColumn("Message", "Text");
            gridView.Columns.Add(messageColumn);
            Binding binding = new Binding();
            binding.Source = messages;
            listView.SetBinding(ItemsControl.ItemsSourceProperty, binding);
            MainGrid.Children.Add(listView);
        }

        private static GridViewColumn CreateGridViewColumn(string header, string bindingPath)
        {
            GridViewColumn gridViewColumn = new GridViewColumn();
            gridViewColumn.Header = new GridViewColumnHeader() { Content = header };
            string xaml = @"
            <DataTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""> 
              <StackPanel Orientation=""Horizontal"">
                <TextBlock Text=""{Binding Text}"" MaxHeight=""20"">
                  <TextBlock.ToolTip>
                    <TextBlock Text=""{Binding Text}""/>
                  </TextBlock.ToolTip>
                </TextBlock>
                <TextBlock Text="" -- ""/>
                <TextBlock Text=""{Binding Date}""/>
              </StackPanel>
            </DataTemplate>";
            StringReader stringReader = new StringReader(xaml);
            XmlReader xmlReader = XmlReader.Create(stringReader);
            gridViewColumn.CellTemplate = XamlReader.Load(xmlReader) as DataTemplate;

            return gridViewColumn;
        }

        public class Message
        {

            public Message(DateTime aDate, String aText)
            {
                Date = aDate;
                Text = aText;
            }

            public DateTime Date { get; set; }
            public String Text { get; set; }
        }
    }
}