如何使用mvvm模型中的wpf中Datagrid的datatemplate中的Textbox提供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

查看:153
本文介绍了如何使用mvvm模型中的wpf中Datagrid的datatemplate中的Textbox提供ID,以便区分文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的WPF应用程序中,我使用MVVM模型。 Datagrid包含文本框和标签,当在文本框中的运行时提供输入时,动态描述将按照同一行中的输入显示在标签中。



但是问题是当我将输入提供给一个文本框时,datagrid中的所有文本框都会反映出与它们的id在网格中不同的输入值。如何解决这个问题。

 < Grid> 
< DataGrid Name =c1DataGrid1ItemsSource ={Binding CreditInfo}AutoGenerateColumns =FalseCanUserAddRows =False>
< DataGrid.Columns>

< DataGridTextColumn Header =CreditBinding ={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标题=描述>
< DataGridTemplateColumn.CellTemplate>
< DataTemplate>
< Grid>
< TextBlock Width =440Text ={Binding PercentageDescription}/>
< / Grid>
< / DataTemplate>
< /DataGridTemplateColumn.CellTemplate>
< / DataGridTemplateColumn>
< /DataGrid.Columns>
< / DataGrid>
< / Grid>


解决方案

如果我正确理解你的代码,网格3列。第一列包含一些值,第二列包含文本框,您可以在其中插入一个值,第三列包含一个文本框,该文本框计算以第二列值为百分比的第一列值的百分比。



ie您的Credit = 50,您可以在第二列的文本框中输入10,并希望5显示在第三列。



如果这是正确的,那么有一个更简单的方法来实现你要什么。



您可以在视图模型中为绑定到网格的项目创建两个新属性。第一个属性将包含在第二列文本框中输入的内容:

  private int _creditPercentage; 

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

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

第二个属性将包含计算:

  public String PercentageDescription 
{
get {return Convert.ToString(Math.Round双)信用*百分比/ 100),CultureInfo.InvariantCulture); }
}

现在,将Percentage属性绑定到第二列中的TextBox。和PercentageDescription到你的第三列:

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

< DataGridTextColumn Header =描述Binding ={Binding Path = PercentageDescription}/>

您可能还需要在第二列的文本框中实现一些输入验证,以确保用户可以只能输入数字。


In My WPF Application I am using MVVM Model. 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.

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. 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. 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.

i.e. you have Credit=50 you type 10 into the second column's textbox and you want 5 to appear in the third column.

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. 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.

这篇关于如何使用mvvm模型中的wpf中Datagrid的datatemplate中的Textbox提供ID,以便区分文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆