I'm new to WPF. In my application I'm trying to calcualte the datagrid c开发者_开发知识库olumn based on another column and TextBox. it should multiply the two and give the result. For the same I'm using the IMultiValueConverter. I'm in DataGrid the Bindings are as follows:
<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>
The Problem is my Element Binding. The Element Name is absolutely correct but in my implemented class it always take the value as DependencyProperty.UnsetValue. What is the Problem??? Any idea.
The Converter Function is as follows :
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;
}
}
ElementName
binding sources search for the source element in the current NAME SCOPE. This is similar to scopes in other languages (like C# or VB). Scopes are usually defined with each unique XAML file (Window
/UserControl
), but templates (ControlTemplates
and DataTemplates
) have their own scope. There are other controls that also define their own name scope, like ContextMenu
.
It looks like the text box that you are referring to is in a different name scope.
精彩评论