无法从Xaml到ObservableCollection工作获得双向绑定

时间:2015-03-06 14:11:30

标签: c# binding windows-store-apps winrt-xaml inotifypropertychanged

我试图将gridView绑定到Windows应用商店应用中的ObservableCollection。我的问题是我得到的数据,但设置似乎不起作用

public class ValveData : INotifyPropertyChanged
{
    private string valveName;
    private decimal waterMl;
    private decimal waterEc;
    private decimal waterPh;


    public string ValveName 
    {
        get { return this.valveName; }
        set
        {
            this.valveName = value;
            this.OnPropertyChanged("ValveName");
        }
    }

    public decimal WaterMl
    {
        get { return this.waterMl; }
        set
        {
            this.waterMl = value;
            this.OnPropertyChanged("WaterMl");
        }
    }

    public decimal WaterEc
    {
        get { return this.waterEc; }
        set
        {
            this.waterEc = value;
            this.OnPropertyChanged("WaterEc"); 
        }
    }

    public decimal WaterPh
    {
        get { return this.waterPh; }
        set
        {
            this.waterPh = value;
            this.OnPropertyChanged("WaterPh");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

public class ValveDataCollection : ObservableCollection<ValveData>
{
    public ValveDataCollection() : base()
    {
        Add(new ValveData { ValveName = "Valve 1", WaterMl = 100, WaterEc = 3, WaterPh = 5 });
        Add(new ValveData { ValveName = "Valve 2" });
        Add(new ValveData { ValveName = "Valve 3" });
        Add(new ValveData { ValveName = "Valve 4" });
    }
}

这是我的Xaml

<data:ValveDataCollection x:Key="ValvesData"/>

<GridView x:Name="gw1" Grid.Row="2" ItemsSource="{StaticResource ValvesData}">
        <GridView.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBox Text="{Binding ValveName, Mode=TwoWay}"/>
                    <TextBox Text="{Binding WaterMl, Mode=TwoWay}"/>
                    <TextBox Text="{Binding WaterEc, Mode=TwoWay}"/>
                    <TextBox Text="{Binding WaterPh, Mode=TwoWay}"/>
                </StackPanel>
            </DataTemplate>
        </GridView.ItemTemplate>
</GridView>

我确定我遗漏了一些东西,但我已经看了好几天这个代码而无法弄明白 该字符串看起来像它的工作,这意味着如果我有2个文本框链接到ValveName字符串设置字符串并获取数据,但它看起来不适用于小数,他们在启动时获取数据,但如果您在文本框似乎不会影响变量

1 个答案:

答案 0 :(得分:2)

正确的XAML应该看起来像

<TextBox Text="{Binding ValveName, Mode=TwoWay}"/>

而不是

<TextBox Text="{Binding ValveName}, Mode=TwoWay"/>

修改 旁注:

此代码不是线程安全的。在this.PropertyChanged != nullthis.PropertyChanged之间,另一个帖子可以取消订阅,PropertyChanged会变为null

public void OnPropertyChanged(string propertyName)
{
    if (this.PropertyChanged != null)
    {
        this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

始终使用事件处理程序的本地副本!

public void OnPropertyChanged(string propertyName)
{
    var handler = this.PropertyChanged;
    if (handler != null)
    {
        handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

这个线程现在是安全的

对于未来的读者:使用C#6.0(Visual Studio 2015),您还可以使用以下版本,该版本更短线程安全,使用新的&#34; Null Propagation Operator&#34;运营商。

public void OnPropertyChanged(string propertyName)
{
    this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}