본문 바로가기

Windows

C# WPF ListView Column Header Tooltip.

반응형

C# WPF에서 ListView를 동적으로 만드는 경우 Column Header에 Tooltip을 추가하는 방법입니다.

        private void GridViewColumnHeader_Loaded(object sender, RoutedEventArgs e)
        {
            GridViewColumnHeader columnHeader = sender as GridViewColumnHeader;
            if (columnHeader.Content == null)
                return;
            string strHeader = columnHeader.Content as string;

            ColumnItem item = Array.Find(cColumnItems, x => x.strHeader.Equals(strHeader));
            columnHeader.ToolTip = item.strTooltip;
        }
    

ListView 동적으로 만드는 소스도 첨부합니다.

샘플은 DataTemplate도 동적으로 만들어 본 예제입니다.

 

1. SampleList.xaml

<Page>
    <Page.Resources>
	    <Style x:Key="SampleListHeaderStyle" TargetType="{x:Type TextBlock}">
            <Setter Property="FontFamily" Value="Arial" />
            <Setter Property="FontSize" Value="11" />
            <Setter Property="FontWeight" Value="Bold" />
            <Setter Property="Foreground" Value="#1b4454" />
            <Setter Property="VerticalAlignment" Value="Center" />
            <Setter Property="TextAlignment" Value="Center" />
        </Style>

	    <Style x:Key="SampleHeaderStyle" TargetType="{x:Type GridViewColumnHeader}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type GridViewColumnHeader}">
                        <Border BorderThickness="1 0 0 1"
                                BorderBrush="#7697a2"
                                Background="#e3edf1"
                                Height="28"
                                >
                            <TextBlock x:Name="ContentHeader"
                                       Text="{TemplateBinding Content}"
                                       Width="{TemplateBinding Width}"
                                       Style="{StaticResource SampleListHeaderStyle}" />
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="Foreground" Value="#1b4454" />
            <EventSetter Event="Loaded" Handler="GridViewColumnHeader_Loaded"/>
        </Style>
    </Page.Resources>
    <Grid>
        <ListView x:Name="SampleListView" Style="{StaticResource SampleListStyle}" Margin="15 8 15 10"
                  ScrollViewer.VerticalScrollBarVisibility="Visible" ScrollViewer.HorizontalScrollBarVisibility="Visible">
            <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                    <Setter Property="VerticalContentAlignment" Value="Stretch" />
                    <Setter Property="BorderBrush" Value="#c6dce5"/>
                    <Setter Property="BorderThickness" Value="0 0 0 1"/>
                    <Setter Property="Height" Value="28" />
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="#f7f7e9"/>
                        </Trigger>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="Background" Value="#f7f7e9"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </ListView.ItemContainerStyle>
            <ListView.View>
                <GridView x:Name="SampleListGridView" AllowsColumnReorder="False"
                          ColumnHeaderContainerStyle="{StaticResource SampleHeaderStyle}" />
            </ListView.View>
        </ListView>
    <Grid>
</Page>

 

2. SampleList.xaml.cs

namespace SampleList
{
    public partial class SampleList : Page
    {
        ListItem[] cListItems = new ListItem[]
        {
            new ListItem("Time", "Start Time", "StartTime", "StringFormat='yyyy-MM-dd HH:mm:ss.fff'", 147, "Right"),
            new ListItem("Value1", "Double Value 1", "Delay2", "StringFormat='{}{0:0.00}'", 60, "Right"),
            new ListItem("Value2", "Double Value 2", "Delay4", "StringFormat='{}{0:0.00}'", 60, "Right"),
            new ListItem("Value3", "Double Value 3", "Delay7A", "StringFormat='{}{0:0.00}'", 60, "Right"),
        }
		
        private List<OptimalControlHistoryItem> listOptimalControlHistory;

        public SampleList()
        {
            InitializeComponent();
            for (int i = 0; i < cListItems.Length; i++)
            {
                GridViewColumn tColumn = new GridViewColumn();
                tColumn.Header = cListItems[i].strHeader;
                tColumn.Width = cListItems[i].width;
                tColumn.CellTemplate = getDataTemplate(i);
                SampleListGridView.Columns.Add(tColumn);
            }
            SampleListView.ItemsSource = listOptimalControlHistory;
        }

        private DataTemplate getDataTemplate(int iIndex)
        {
            var xamlString =
            "<DataTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">" +
                "<Border BorderBrush=\"#c6dce5\" BorderThickness=\"0 0 1 0\" Margin=\"-6,-2,-8,-2\">" +
                    "<StackPanel Margin=\"6,2,8,2\" VerticalAlignment=\"Center\">" +
                        "<TextBlock Text=\"{Binding " + cListItems[iIndex].strNames + ", "+
                            cListItems[iIndex].strFormat + "}\" " +
                            "TextAlignment=\"" + cListItems[iIndex].strAlignment +
                            "\" Style =\"{DynamicResource OptimalControlHistoryListTextStyle}\"/>" +
                    "</StackPanel>" +
                "</Border>" +
            "</DataTemplate>";
            StringReader stringReader = new StringReader(xamlString);
            XmlReader xmlReader = XmlReader.Create(stringReader);
            DataTemplate template = (DataTemplate)XamlReader.Load(xmlReader);
            return template;
        }

        private void GridViewColumnHeader_Loaded(object sender, RoutedEventArgs e)
        {
            GridViewColumnHeader columnHeader = sender as GridViewColumnHeader;
            if (columnHeader.Content == null)
                return;
            string strHeader = columnHeader.Content as string;

            //HistoryItems item = cListItems.Find(x => x.strHeader.Equals(strHeader));
            HistoryItem item = Array.Find(cHistoryItems, x => x.strHeader.Equals(strHeader));
            columnHeader.ToolTip = item.strTooltip;
        }
    }

    public class ListItem
    {
        private string _Header;
        public string strHeader;
        public string strTooltip;
        public string strNames;
        public string strFormat;
        public int width;
        public string strAlignment;
        public HistoryItem(string v1, string v2, string v3, string v4, int v5, string v6)
        {
            strHeader = v1;
            strTooltip = v2;
            strNames = v3;
            strFormat = v4;
            width = v5;
            strAlignment = v6;
        }
    }
}

 

반응형

'Windows' 카테고리의 다른 글

office 2019 odt 설치  (0) 2021.01.25
c# WPF UI 작업관련  (0) 2020.10.27
Ahnlab Safe Transaction  (2) 2020.08.25
Windows10에서 메모장이 사라진 경우  (0) 2020.08.04
OPC.DA x64 버전  (0) 2020.07.30