簡體   English   中英

WPF ListView不會在PropertyChanged上更新

[英]WPF ListView doesn't update on PropertyChanged

我有一個WPF列表視圖,該視圖顯示材料,厚度和組合框中的厚度單位……xaml看起來像這樣(為清晰起見,我取消了所有可視化設置):

    <ListView ItemsSource="{Binding Path=MaterialLayers}" IsSynchronizedWithCurrentItem="True">
        <ListView.Resources>
            <x:Array x:Key="DistanceUnitItems" Type="sys:String" xmlns:sys="clr-namespace:System;assembly=mscorlib">
                <sys:String>cm</sys:String>
                <sys:String>inches</sys:String>
            </x:Array>
            <DataTemplate x:Key="ThicknessUnit">
                <ComboBox ItemsSource="{StaticResource DistanceUnitItems}" SelectedIndex="{Binding ThicknessUnit}" />
            </DataTemplate>
        </ListView.Resources>
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Material Name" DisplayMemberBinding="{Binding MaterialName}"/>
                <GridViewColumn Header="Material Thickness" DisplayMemberBinding="{Binding MaterialThickness}"/>  />
                <GridViewColumn Header="Thickness Unit" CellTemplate="{StaticResource ThicknessUnit}"  />
            </GridView>
        </ListView.View>
    </ListView>

MaterialLayers是一個ObservableCollection<MaterialLayer>

MaterialLayer具有MaterialName,MaterialThickness和ThicknessUnit的屬性(對於cm為0,對於英寸為1)。 MaterialThickness將內部存儲的值(以厘米為單位)轉換為ThicknessUnit指定的單位。

更改ThicknessUnit時,我的DataViewModel調用屬性名稱為“ MaterialLayers”的PropertyChanged事件處理程序。

因此,我希望在更改ThicknessUnit時MaterialThickness能夠自動更新。

我已對其進行調試,然后調用PropertyChanged(“ MaterialLayers”)。(調用ThicknessUnit set方法時,它將調用父數據類(MyData)上的事件,該事件將調用DataViewModel上的事件,該事件調用PropertyChanged處理程序)

來自DataViewModel的相關代碼

    public delegate void DataChangedHandler(String identifier);

    public DataViewModel()
    {
        Data = new MyData();
        Data.DataChanged += new DataChangedHandler(RaisePropertyChanged);
    }

    public ObservableCollection<XTRRAMaterialLayer> MaterialLayers
    {
        get { return _data.MaterialLayers; }
        set { }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void RaisePropertyChanged(string propertyName)
    {
        // take a copy to prevent thread issues
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

來自MyData的相關代碼

public class XTRRAData
{
    public ObservableCollection<XTRRAMaterialLayer> MaterialLayers { get; set; }

    public event DataChangedHandler DataChanged;



    public MyData()
    {
        MaterialLayers = new ObservableCollection<MaterialLayer>();
    }

    public void myDataChanged(String identifier)
    {
        DataChanged(identifier);
    }

    public void AddLayer()
    {
        MaterialLayer layer = new MaterialLayer() { MaterialName="Test", MaterialThickness=5, ThicknessUnit=0 };

        layer.DataChanged += new DataChangedHandler(myDataChanged); 
    }
}

MaterialLayer的相關代碼

public class XTRRAMaterialLayer
{
    public XTRRAMaterialLayer()
    {
        _thicknessUnit = 0; // cm
    }
    public event DataChangedHandler DataChanged;

    public String MaterialName { get; set; }

    public double MaterialThickness
    {
        get
        {
            switch (_thicknessUnit)
            {
                default:
                case 0:
                    return DIST;
                case 1:
                    return DIST * 0.393700787;
            }
        }
        set
        {
            switch (_thicknessUnit)
            {
                default:
                case 0:
                    DIST = value;
                    break;
                case 1:
                    DIST = value / 0.393700787;
                    break;
            }

        }
    }

    public int _thicknessUnit;

    public int ThicknessUnit
    {
        get { return(int) _thicknessUnit;  }
        set
        {
            _thicknessUnit = (eThicknessUnits)value;
            FireDataChanged("MaterialLayers");
        }
    }

    public void FireDataChanged(String identifier)
    {
        if(DataChanged!=null)
        {
            DataChanged(identifier);
        }
    }

}

有人可以幫忙嗎?

您需要在要更改屬性的類中實現INotifyPropertyChanged-在您的情況下為XTRRAMaterialLayer,並在屬性更改時引發PropertyChanged事件。

您的屬性應如下所示:

public int ThicknessUnit
{
    get { return(int) _thicknessUnit;  }
    set
    {
        _thicknessUnit = (eThicknessUnits)value;
        NotifyPropertyChanged();
    }
}

NotifyPropertyChanged事件處理程序:

public event PropertyChangedEventHandler PropertyChanged;

// This method is called by the Set accessor of each property. 
// The CallerMemberName attribute that is applied to the optional propertyName 
// parameter causes the property name of the caller to be substituted as an argument. 
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
    if (PropertyChanged != null)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM