繁体   English   中英

无法从Xaml到ObservableCollection的双向绑定

[英]Cant get twoWay Binding from Xaml to ObservableCollection Working

我试图将gridView绑定到Windows Store应用程序中的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字符串,则设置字符串并获取数据,但是似乎无法使用小数点,它们会在启动时获取数据,但是如果您在文本框中进行修改,似乎不会影响该变量

正确的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),您还可以使用以下版本,该版本更短线程安全,并且使用了新的“空传播运算符”运算符。

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

暂无
暂无

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

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