如何在DataGridTextColumn上使用MultiBinding? [英] How to use a MultiBinding on DataGridTextColumn?
本文介绍了如何在DataGridTextColumn上使用MultiBinding?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的 ImageDimension $ c中的
高度
和宽度
$ c>对象,我想绑定在一起,所以它显示的东西像50x60(ax之间的字符)?我该如何实现?下面的代码给我一个错误说
I have 2 properties as Height
and Width
on my ImageDimension
object and I want to bind them together so it displays something like 50x60 (a x character in between)? How can I achieve this? The Code below gives me an error saying
对象引用未设置为对象实例。
"Object reference not set to an object instance."
<cst:CustomDataGrid x:Name="grdImageDimension"
ItemsSource="{Binding ImageDimensions, IsAsync=True}"
<DataGridTextColumn Header="ImageDimension" Width="50">
<DataGridTextColumn.Binding>
<MultiBinding StringFormat="{}{0} + {1}">
<Binding Path="ImageDimensions.Height" />
<Binding Path="ImageDimensions.Width" />
</MultiBinding>
</DataGridTextColumn.Binding>
</DataGridTextColumn>
</cst:CustomDataGrid>
ViewModel:
ViewModel:
Public Class ImageDimensionsVM
Private m_ImageDimensions As ObservableCollection(Of ImageDimension)
Public Property ImageDimensions() As ObservableCollection(Of ImageDimension)
Get
Return m_ImageDimensions
End Get
Set(value As ObservableCollection(Of ImageDimension))
m_ImageDimensions = value
End Set
End Property
推荐答案
如果要将数据绑定到 ImageDimension
对象,直接使用它们作为@Giangregorio指出:
If you want to data bind to the properties of the ImageDimension
object, just use them directly as @Giangregorio points out:
<Window x:Class="DataGridTextHeightWidth.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>
<DataGrid x:Name="grdImageDimension" ItemsSource="{Binding
ImageDimensions, IsAsync=True}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn x:Name="MyGridColumn"
Header="ImageDimension"
Width="*">
<DataGridTextColumn.Binding>
<MultiBinding StringFormat="{}{0} x {1}">
<Binding Path="Height" />
<Binding Path="Width" />
</MultiBinding>
</DataGridTextColumn.Binding>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
代码背后:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
// Create list
MyImageDimensionCol col = new MyImageDimensionCol();
col.ImageDimensions = new ObservableCollection<ImageDimension>();
col.ImageDimensions.Add(new ImageDimension() { Height = 5, Width = 10 });
col.ImageDimensions.Add(new ImageDimension() { Height = 15, Width = 20 });
col.ImageDimensions.Add(new ImageDimension() { Height = 5, Width = 5 });
DataContext = col;
}
}
public class MyImageDimensionCol
{
public ObservableCollection<ImageDimension> ImageDimensions { get; set; }
}
public class ImageDimension
{
public int Height { get; set; }
public int Width { get; set; }
}
这篇关于如何在DataGridTextColumn上使用MultiBinding?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文