簡體   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