简体   繁体   English

如何在mvvm模型中的wpf中为Datagrid的数据模板内部的文本框提供ID,以便我可以区分文本框

[英]How to provide the ID to the Textbox that is inside the Datagrid's datatemplate in wpf in mvvm model, So that I can differentiate the textbox

In My WPF Application I am using MVVM Model. 在我的WPF应用程序中,我正在使用MVVM模型。 Datagrid Contains Textbox and Label, when provide the input at the run time in the Textbox, dynamically a description will show in label as per the input in the same row. Datagrid包含Textbox和Label,当在运行时在Textbox中提供输入时,将根据同一行中的输入在标签中动态显示说明。

But the problem is when I provided the input to a textbox, all the textbox with in the datagrid reflect the same input value as their id is not different in grid. 但是问题是当我将输入提供给文本框时,datagrid中所有具有文本框的文本框都反映相同的输入值,因为它们的id在网格中没有区别。 how can I solve this problem. 我怎么解决这个问题。

<Grid>
    <DataGrid Name="c1DataGrid1" ItemsSource="{Binding CreditInfo}" AutoGenerateColumns="False" CanUserAddRows="False">
        <DataGrid.Columns>

            <DataGridTextColumn  Header="Credit" Binding="{Binding Path=Credit}"/>
            <DataGridTemplateColumn Header="Percentage">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Grid>

                            <TextBox Text="{Binding Path=DataContext.CreditPercentage, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" />
                            <b:Interaction.Triggers>
                                <b:EventTrigger EventName="LostFocus">
                                    <b:InvokeCommandAction  Command="{Binding Path= DataContext.LostFocusCommand, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" CommandParameter="{Binding}">                                          
                                    </b:InvokeCommandAction>
                                </b:EventTrigger>
                            </b:Interaction.Triggers>
                        </Grid>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn Header="Description">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Grid>
                            <TextBlock Width="440" Text="{Binding PercentageDescription}"/>
                        </Grid>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
</Grid> 

If I've understood your code correctly you have a grid with 3 columns. 如果我正确理解了您的代码,那么您将获得一个包含3列的网格。 The first column contains some value the second column contains textbox where you can insert a value and the third column contains a textbox that calculates percentage of the first column value taking second column value as the percent. 第一列包含某个值,第二列包含可在其中插入值的文本框,第三列包含一个文本框,该文本框以第二列值作为百分比来计算第一列值的百分比。

ie you have Credit=50 you type 10 into the second column's textbox and you want 5 to appear in the third column. 例如,您有Credit = 50,则在第二列的文本框中键入10,并希望5出现在第三列中。

If that's correct then there is an easier way to achieve what you want. 如果这是正确的,那么有一种更轻松的方法来实现您想要的。

You create two new properties in the view model for the items bound to your grid. 您在视图模型中为绑定到网格的项目创建了两个新属性。 The first property will contain whatever is entered into the textbox of the second column: 第一个属性将包含在第二列的文本框中输入的内容:

    private int _creditPercentage;

    public int CreditPercentage
    {
        get { return _creditPercentage; }
        set
        {
            if (value == _creditPercentage)
                return;

            _creditPercentage= value;
            OnPropertyChanged("CreditPercentage");
            OnPropertyChanged("PercentageDescription");
        }
    }

The second property is going to contain the result of the calculation: 第二个属性将包含计算结果:

public String PercentageDescription
{
    get { return Convert.ToString(Math.Round((double)Credit*Percentage/100), CultureInfo.InvariantCulture); }
}

Now you bind the Percentage property to your TextBox in the second column. 现在,将Percentage属性绑定到第二列中的TextBox。 And PercentageDescription to your third column: 第三列的百分比说明:

<DataGridTemplateColumn Header="Percentage">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBox Text="{Binding CreditPercentage}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>

<DataGridTextColumn  Header="Description" Binding="{Binding Path=PercentageDescription}"/>

You might also want to implement some input validation in that textbox in the second column to insure that user can only enter digits. 您可能还希望在第二列的该文本框中实施一些输入验证,以确保用户只能输入数字。

its worked for me by applying the OnPropertyChanged("CreditPercentage"); 它通过应用OnPropertyChanged(“ CreditPercentage”)为我工作; in the creditinfo property, also define the percentageDescription property in creditmodel. 在creditinfo属性中,还要在creditmodel中定义percentDescription属性。

 public ObservableCollection<Credits> CreditInfo
    {
        get
        {
            return infos;
        }
        set
        {

            infos = value;
            OnPropertyChanged("CreditInfo");
            OnPropertyChanged("CreditPercentage");
            //OnPropertyChanged("PercentageDescription");
        }
    }public string PercentageDescription
    {
        get
        {
            return percentageDescription;
        }
        set
        {
            percentageDescription = value;
            OnPropertyChanged("PercentageDescription");
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM