当我以MVVM方式将ObservableCollection绑定到Grid时,我想在我的列标题中添加一个空格。这是我的代码..
public class Site
{
public string Name { get; set; }
public string Description { get; set; }
[Description("Short Code")]
[Bindable(true)]
public string ShortCode { get; set; }
public static ObservableCollection<Site> GetSampleData()
{
var sites = new ObservableCollection<Site>();
sites.Add(new Site { Name = "New Delhi", Description = "New Delhi Railway Station", ShortCode = "NDLS"});
sites.Add(new Site { Name = "Mumbai", Description = "Mumbai Railway Station", ShortCode = "MUM"});
return sites;
}
}
//这是我的View Model Class
public class SiteViewModel
{
public ObservableCollection<Site> SitesDataCollection { get; set; }
public SiteViewModel()
{
SitesDataCollection = Site.GetSampleData();
}
}
//这是视图类
public partial class SiteView : UserControl
{
public Sites()
{
InitializeComponent();
DataContext = new SiteViewModel();
}
}
我的XAML
<Grid>
<DockPanel>
<DataGrid Name="SiteGrid" ItemsSource="{Binding Path=SitesDataCollection}">
</DataGrid>
</DockPanel>
</Grid>
基本上在我的网格中,对于第三列,标题是ShortCode,我想显示&#34;短代码&#34;,我该怎么做。
我尝试添加
[Description("Short Code")] and
[Bindable(true)]
但没有得到理想的结果。
答案 0 :(得分:0)
在DataGrid的AutoGeneratingColumn事件处理程序中,通过引用DataGridAutoGeneratingColumnEventArgs.Column属性来访问DataGridColumn属性。使用此选项编辑列标题。
private void DataGrid_OnAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
e.Column.Header = Regex.Replace(e.Column.Header.ToString(), "(\\B[A-Z])", " $1");
}
这将在您的属性名称中添加空格,其中包含大写字母。