转换函数始终具有一个值 DependencyProperty.UnsetValue - WPF
我是 WPF 新手。在我的应用程序中,我尝试根据另一列和文本框计算数据网格列。它应该将两者相乘并给出结果。同样,我正在使用 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;
}
}
I'm new to WPF. In my application I'm trying to calcualte the datagrid column 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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ElementName
绑定源在当前 NAME SCOPE 中搜索源元素。这与其他语言(如 C# 或 VB)中的作用域类似。范围通常是使用每个唯一的 XAML 文件 (Window
/UserControl
) 定义的,但模板(ControlTemplates
和DataTemplates
)有自己的范围。还有其他控件也定义了自己的名称范围,例如 ContextMenu。您所引用的文本框似乎位于不同的名称范围内。
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
andDataTemplates
) have their own scope. There are other controls that also define their own name scope, likeContextMenu
.It looks like the text box that you are referring to is in a different name scope.