我是 WPF 的新手。在我的应用程序中,我试图根据另一列和 TextBox 计算数据网格列。它应该将两者相乘并给出结果。同样,我正在使用 IMultiValueConverter。我在 DataGrid 中的绑定如下:
<DataGrid.Columns>
<DataGridTemplateColumn Header="TranD">
<DataGridTemplateColumn.CellTemplate >
<DataTemplate>
<TextBox Text="{Binding Path=TranD, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, TargetNullValue=0}" Name="dgtbTranD"
/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=TranD, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, TargetNullValue=0}"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Tran Credit" Width="*">
<DataGridTemplateColumn.CellTemplate >
<DataTemplate>
<TextBox Text="{Binding Path=TranC, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, TargetNullValue=0}" Name="dgctbTranC"
/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=TranC, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, TargetNullValue=0}" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Func Debit" Width="*">
<DataGridTemplateColumn.CellTemplate >
<DataTemplate>
<TextBlock Name="dgtbFuncD" IsEnabled="False" >
<TextBlock.Text>
<MultiBinding Converter="{StaticResource Multiplication}">
<Binding Path="TranD" TargetNullValue="0" />
<Binding ElementName="txtExchRate" Path="Text"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Func Credit" Width="*">
<DataGridTemplateColumn.CellTemplate >
<DataTemplate>
<TextBlock Name="dgctbFuncC" IsEnabled="False">
<TextBlock.Text>
<MultiBinding Converter="{StaticResource Multiplication}">
<Binding Path="TranC" />
<Binding ElementName="txtExchRate" Path="Text" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
问题是我的元素绑定。元素名称是绝对正确的,但在我实现的类中,它总是将值作为 DependencyProperty.UnsetValue。问题是什么???任何想法。
转换器功能如下:
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
try
{
if (values[0] == null || values[1] == null)
{
return 0;
}
else
{
return (System.Convert.ToInt32(values[0]) + System.Convert.ToInt32(values[1])).ToString();
}
}
catch (Exception ex)
{
return 0;
}
}