简体   繁体   English

Silverlight DataGrid双向绑定

[英]Silverlight datagrid twoway binding

I have datagrid in which one of the column is Textbox control, have set the binding as twoway. 我有datagrid列之一是Textbox控件,已将绑定设置为双向。 the grid is binded to a observable collection. 网格被绑定到一个可观察的集合。 Issue : If I enter some value in text box, collection get populated. 问题:如果我在文本框中输入一些值,则会填充集合。 now if I clears that textbox the same has not been reflected to the collection. 现在,如果我清除该文本框,则该文本框不会反映到集合中。 my binded property is nullable. 我的绑定属性可以为空。

Thanks N Advance 谢谢N前进

Check the Output window in Visual Studio for binding errors. 在Visual Studio中检查“输出”窗口是否存在绑定错误。 You should see a warning that there is no implicit conversion from an empty string to an Integer. 您应该看到一条警告,说明没有从空字符串到整数的隐式转换。

You could use a ValueConverter to perform this task. 您可以使用ValueConverter来执行此任务。

See this related question for other approaches. 有关其他方法,请参见此相关问题。 Note that some may not apply as the focus there is WPF. 请注意,有些可能不适用于WPF。

WPF binding not working properly with properties of int type WPF绑定不适用于int类型的属性

您可以使用

<TextBox IsReadOnly="True" Text="{Binding BUApprovalQty, Mode=TwoWay, TargetNullValue=''}"></TextBox>

I'm not sure where your problem might be exactly, but I've tried the following and it works as expected: 我不确定您的问题可能在哪里,但是我尝试了以下操作,它可以按预期工作:

I have a simple Person class: 我有一个简单的Person类:

public class Person : ObservableObject
{
    private string name;
    public string Name
    {
        get { return name; }
        set
        {
            if (name == value)
                return;
            name = value;
            RaisePropertyChanged(() => Name);
        }
    }

    private int? age;
    public int? Age
    {
        get { return age; }
        set
        {
            if (age == value)
                return;
            age = value;
            RaisePropertyChanged(() => Age);
        }
    }
}

I also have a simple ViewModel with an ObservableCollection<Person> called Data . 我也有一个名为ObservableCollection<Person>的简单ViewModel,它称为Data Ans, here's my XAML: 回答,这是我的XAML:

<sdk:DataGrid Name="dataGrid" ItemsSource="{Binding Data}" AutoGenerateColumns="False"
              SelectedItem="{Binding SelectedPerson, Mode=TwoWay}" Grid.Row="1">
    <sdk:DataGrid.Columns>
        <sdk:DataGridTextColumn Binding="{Binding Name, Mode=TwoWay}"/>
        <sdk:DataGridTextColumn Binding="{Binding Age, Mode=TwoWay}"/>
    </sdk:DataGrid.Columns>
</sdk:DataGrid>

I checked how it's working by setting a breakpoint in the Setter for my Age property, and it shows the value comming in as null 我通过在我的Age属性的Setter中设置一个断点来检查它的工作方式,它显示的值为null

Hope this helps ;) 希望这可以帮助 ;)

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

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