I'm having a DataGrid, I wish to append the Column Header Text in each value in a Cell
我有一个DataGrid,我希望在单元格中的每个值中添加列标题文本
The XAML Source Code is
XAML的源代码是
<Window x:Class="DataGrid_Index.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"
xmlns:cust="clr-namespace:DataGrid_Index" >
<Grid>
<DataGrid Name="Grid1" AutoGenerateColumns="False" ItemsSource="{Binding MobileList, UpdateSourceTrigger=PropertyChanged}" IsReadOnly="True" SelectionUnit="Cell" SelectionChanged="Grid1_SelectionChanged" >
<DataGrid.Columns>
<!--Column 1-->
<DataGridTextColumn Binding="{Binding MobileName}" Header="Name" />
<!--Column 2-->
<DataGridTextColumn Binding="{Binding MobileOS}" Header="OS" />
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
The C# View Model is
c#视图模型是
public class MobileVM : ViewModel
{
private ObservableCollection<Mobile> _mobileList = new ObservableCollection<Mobile>();
public ObservableCollection<Mobile> MobileList
{
get { return _mobileList; }
set { _mobileList = value; OnPropertyChanged(); }
}
public MobileVM()
{
MobileList = new ObservableCollection<Mobile>()
{
new Mobile() { MobileName = "iPhone 6", MobileOS = "IOS" },
new Mobile() { MobileName = "Galaxy S6", MobileOS = "Android" },
new Mobile() { MobileName = "Lumina", MobileOS = "Windows" }
};
}
}
public class Mobile
{
public string MobileName { get; set; }
public string MobileOS { get; set; }
}
I'm expecting output
我期望输出
Name OS
_________________________________________
Name: iPhone 6 OS: IOS
Name: Galaxy S6 OS: Android
Name: Lumina OS: Windows
How to bind the Column.Header
in a Cell ? Kindly assist me.
如何绑定列。单元格中的头?请帮助我。
1 个解决方案
#1
0
You can achieve that with a MultiBinding like this:
您可以通过这样的多绑定实现:
<DataGridTextColumn Header="Name" x:Name="NameC">
<DataGridTextColumn.Binding>
<MultiBinding StringFormat=" {0}: {1}">
<Binding ElementName="NameC" Path="Header"></Binding>
<Binding Path="MobileName"></Binding>
</MultiBinding>
</DataGridTextColumn.Binding>
</DataGridTextColumn>
The StringFormat
allows you to some more customization.
StringFormat允许您进行更多的定制。
Hope this helps
希望这有助于
#1
0
You can achieve that with a MultiBinding like this:
您可以通过这样的多绑定实现:
<DataGridTextColumn Header="Name" x:Name="NameC">
<DataGridTextColumn.Binding>
<MultiBinding StringFormat=" {0}: {1}">
<Binding ElementName="NameC" Path="Header"></Binding>
<Binding Path="MobileName"></Binding>
</MultiBinding>
</DataGridTextColumn.Binding>
</DataGridTextColumn>
The StringFormat
allows you to some more customization.
StringFormat允许您进行更多的定制。
Hope this helps
希望这有助于