问题描述
对于应用程序,我需要使用 DataGridTextColumn
创建具有 MultiBinding的
。 DataGrid
第一个 Binding
使用ItemsSource中给出的属性,第二个 Binding
应该使用我的属性ViewModel。
For an application I need to create a DataGrid
with a DataGridTextColumn
which has MultiBinding
.The first Binding
uses a property given in the ItemsSource, the second Binding
should use a Property from my ViewModel.
<DataGridTextColumn Header="Hourly wage" SortMemberPath="HourlyWage">
<DataGridTextColumn.Binding>
<MultiBinding StringFormat="{}{0}{1}">
<Binding Path="HourlyWage" />
<Binding Path="CurrencyUnit" />
</MultiBinding>
</DataGridTextColumn.Binding>
</DataGridTextColumn>
在这种情况下,小时工资
是当前项目。 CurrencyUnit
是我的ViewModel中的一个属性。
如果我启动我的应用程序,此列为空。
In that case HourlyWage
is a property of the current item. CurrencyUnit
is a property in my ViewModel.If I start my application this column is empty. So how do I get it displaying both?
我使用Catel作为我的MVVM框架,并使用MahApps.Metro作为我的GUI。我无法在视图中创建ViewModel,因为它是由Catel处理的。
I'm using Catel as my MVVM Framework and MahApps.Metro for my GUI. I can't create a ViewModel in my View as it is handled by Catel.
问候,
Stefan
Regards,Stefan
推荐答案
如果无法访问视图模型,则可以如下定义第二个绑定:
If you cannot access the view model, you can define the second binding as follows:
<Binding RelativeSource="{RelativeSource AncestorType=DataGrid}"
Path="DataContext.CurrencyUnit" />
绑定的源将设置为DataGrid实例。假设您的视图模型保存了以下项的路径,则路径 DataContext.CurrencyUnit
将引用属性 YourViewModel.CurrencyUnit
。数据网格和货币单位属性。
The source of the Binding will be set to the DataGrid instance. The path DataContext.CurrencyUnit
will refer to the property YourViewModel.CurrencyUnit
, assuming that your view model holds the items collection for the data grid and the currency unit property.
这篇关于绑定DataGridTextColumn中的ViewModel属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!