转换函数始终具有一个值 DependencyProperty.UnsetValue - WPF

发布于 2024-10-26 08:09:28 字数 4657 浏览 4 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盗梦空间 2024-11-02 08:09:28

ElementName 绑定源在当前 NAME SCOPE 中搜索源元素。这与其他语言(如 C# 或 VB)中的作用域类似。范围通常是使用每个唯一的 XAML 文件 (Window/UserControl) 定义的,但模板(ControlTemplatesDataTemplates)有自己的范围。还有其他控件也定义了自己的名称范围,例如 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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文