C# WPF数据绑定Header中下划线怎么显示出来啊?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# WPF数据绑定Header中下划线怎么显示出来啊?相关的知识,希望对你有一定的参考价值。

第一步,先定义要绑定列表需要的集合数据的类
public class FtpFileInformation


private bool isDirectory;
private string filename;
private string filetime;
private int filesize;

public bool IsDirectory

get return isDirectory;
set

isDirectory = value;
this.OnPropertyChanged(IsDirectory.ToString());


public string Filename

get return filename;
set

filename = value;
this.OnPropertyChanged(Filename);


public string Filetime

get return filetime;
set

filetime = value;
this.OnPropertyChanged(Filetime);



public int Filesize

get return filesize;
set

filesize = value;
this.OnPropertyChanged(Filesize.ToString());//这个一定要有,否则集合变化了,界面不能及时刷新。


public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)

if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));


第二步,定义要绑定的listview
<ListView Name="lvFtpExplorer" IsSynchronizedWithCurrentItem="True" Grid.Column="1" Grid.Row="2" Visibility="Hidden" PreviewMouseDoubleClick="lvFtpExplo
rer_PreviewMouseDoubleClick" ItemsSource="Binding" DataContext="Binding">

<ListView.View>
<GridView >
<GridViewColumn Header="文件类型" Width="60" >
<GridViewColumn.CellTemplateSelector>
<self:ViewListTemplateSelector>
<self:ViewListTemplateSelector.FileItemTemplate>
<DataTemplate>
<Image Source="/DataDownloader;component/Image/file.png" Width="20" Height="20"></Image> //在列中区分显示的图片
</DataTemplate>
</self:ViewListTemplateSelector.FileItemTemplate>
<self:ViewListTemplateSelector.FolderItemTemplate>
<DataTemplate>
<Image Source="/DataDownloader;component/Image/Folder.png" Width="20" Height="20"></Image>//在列中区分显示的图片
</DataTemplate>
</self:ViewListTemplateSelector.FolderItemTemplate>
</self:ViewListTemplateSelector>
</GridViewColumn.CellTemplateSelector>
</GridViewColumn>
<GridViewColumn Header="文件名" Width="80" DisplayMemberBinding="Binding Path=Filename">
</GridViewColumn> <GridViewColumn Header="文件大小" Width="80" DisplayMemberBinding="Binding Path=Filesize" >
</GridViewColumn> <GridViewColumn Header="最近修改时间" Width="120" DisplayMemberBinding="Binding Path=Filetime">
</GridViewColumn>
</GridView>
</ListView.View>
<ListView.ContextMenu>
<ContextMenu>
<MenuItem Header="下载" Click="AddDownload_Click"/>
<MenuItem Header="复制" />
<MenuItem Header="清除" />
</ContextMenu>
</ListView.ContextMenu>
</ListView>

第三步,在c#后台代码中绑定listview的源ObservableCollection<FtpFileInformation> ftpFileInformationn集合中
private ObservableCollection<FtpFileInformation> ftpFileInformation;//用于绑定ftp浏览文件目录和文件
ftpFileInformation=new ObservableCollection<FtpFileInformation>();
vFtpExplorer.ItemsSource = ftpFileInformation;

第四步,在后台代码中实现列属性区分显示的类
//区分显示文件和文件夹的图标类
public class ViewListTemplateSelector : DataTemplateSelector
public DataTemplate FileItemTemplate get; set;
public DataTemplate FolderItemTemplate get; set;
public override DataTemplate SelectTemplate(object item, DependencyObject container)

FtpFileInformation data = item as FtpFileInformation;
return (data != null && data.IsDirectory ==true) ? FolderItemTemplate : FileItemTemplate;


第五步,现在就可以在listview 的<GridViewColumn Header="文件类型" >的列中
根据绑定数据的FtpFileInformation 的IsDirectory 属性是TRUE和FALSE来在listview
的“文件类型”的列中不同行显示不同的图片。追问

我只要显示下划线,你发这么多给我干嘛用啊?

参考技术A                      <DataGrid >
                    <DataGrid.Columns>
                        <DataGridTextColumn Binding="Binding ABC">
                            <DataGridTextColumn.Header>
                                <TextBlock Text="ABC" TextDecorations="UnderLine"/>
                            </DataGridTextColumn.Header>
                        </DataGridTextColumn>
                        </DataGrid.Columns>
                    </DataGrid>

 抱歉我理解错了上面的是全部加下划线的。

下面的应该是你要的,同样是利用TextBlock 控件

                     <DataGrid >
                    <DataGrid.Columns>
                        <DataGridTextColumn Binding="Binding A_BC">
                            <DataGridTextColumn.Header>
                                <TextBlock Text="A_BC"/>
                            </DataGridTextColumn.Header>
                        </DataGridTextColumn>
                        </DataGrid.Columns>
                    </DataGrid>

本回答被提问者采纳

C# WPF 数据绑定

后台通知:

public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)

if (PropertyChanged != null)

PropertyChanged(this, new PropertyChangedEventArgs(propertyName));

以上是关于C# WPF数据绑定Header中下划线怎么显示出来啊?的主要内容,如果未能解决你的问题,请参考以下文章

C#怎么实现下拉框动态绑定数据

C# WPF datagrid checkboxcolumn 使用问题

关于css中下划线的用法

WPF(C#)中如何让数据库数据逐个显示在textblock中?

WPF C# 数据绑定到 DataGridTextColumn

wpf c# xml修改的方法