wpf 如何在 DataGridTextColumn 上使用 MultiBinding?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33236644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use a MultiBinding on DataGridTextColumn?
提问by batmaci
I have 2 properties as Heightand Widthon my ImageDimensionobject 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
我有2个属性,Height和Width我的ImageDimension目标,我想将它们捆绑在一起,使其显示像50×60(AX字符之间)?我怎样才能做到这一点?下面的代码给了我一个错误说
"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:
视图模型:
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
回答by SwDevMan81
If you want to data bind to the properties of the ImageDimensionobject, just use them directly as @Giangregorio points out:
如果要将数据绑定到ImageDimension对象的属性,只需像@Giangregorio 指出的那样直接使用它们:
<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>
Code behind:
后面的代码:
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; }
}

