PropertyChangedEventHandler为null

时间:2015-10-06 15:56:13

标签: xaml windows-runtime winrt-xaml observablecollection inotifypropertychanged

我有2 ObservableCollection<T>个,每个Card都有public class Card: INotifyPropertyChanged { private string _CardTitle; public string CardTitle { get { return _CardTitle; } set { _CardTitle = value; OnPropertyChanged("CardTitle"); } } public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged(string name) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(name)); } } } 个。

CardTitle

现在当卡片的PropertyChangedEventHandler发生变化时,由于CardTitle,它会反映在用户界面上。

但当我将卡片从一个集合移动到另一个集合,然后更改 handler时,我nullPropertyChangedEventHandler因此,var datatables_options = { "bAutoWidth": true, "sDom": '<"top"i>rt<"bottom"flp><"clear">', "bPaginate": false, "sPaginationType": "full_numbers", "iDisplayLength": 10, "bSort": true, "bFilter": false, "aaSorting": [], "bInfo": false, "bStateSave": false, "iCookieDuration": 0, "bScrollAutoCss": true, "bProcessing": true, "bJQueryUI": false, "sScrollY": "475px", "sScrollX": "100%", "bScrollCollapse": true, "sScrollXInner": "150%" }; var fixedColumnTable = $("table.dataTable").DataTable(datatables_options); new $.fn.dataTable.FixedColumns(fixedColumnTable); 没有被触发,我看不到UI的变化。

我已经摸不着头脑,但无法弄清楚原因。如果有人有任何想法,请在遇到麻烦之前帮助我。

1 个答案:

答案 0 :(得分:0)

你是什么意思&#34;移动&#34;?你从第一个集合中删除,并添加到第二个是?您的datacontext是否实现了INotifyPropertyChanged?

我尝试重现您的代码,但我没有遇到任何问题。看,也许你会发现一些提示:

<强> MainPage.xaml.cs中

public MainPage()
{
    this.InitializeComponent();

    this.NavigationCacheMode = NavigationCacheMode.Required;
    this.DataContext = this;
}

private ObservableCollection<Card> _first;
public ObservableCollection<Card> First
{
    get { return _first; }
    set
    {
        _first = value;
        OnPropertyChanged("First");
    }
}

private ObservableCollection<Card> _second;
public ObservableCollection<Card> Second
{
    get { return _second; }
    set
    {
        _second = value;
        OnPropertyChanged("Second");
    }
}

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    //Init collections
}

private void ChangeItemName(object sender, RoutedEventArgs e)
{
    var card = (sender as Button).DataContext as Card;
    card.CardTitle = ":)";
}

private void ChangeCollection(object sender, RoutedEventArgs e)
{
    var card = (sender as Button).DataContext as Card;
    First.Remove(card);
    Second.Add(card);
    card.CardTitle = "xD";
}

<强> MainPage.xaml中

<StackPanel>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <ListView ItemsSource="{Binding First}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding CardTitle}" />
                        <Button Content="Change name" Click="ChangeItemName" />
                        <Button Content="Change collection" Click="ChangeCollection" />
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

        <ListView ItemsSource="{Binding Second}" Grid.Column="1">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding CardTitle}" />
                        <Button Content="Change name" Click="ChangeItemName" />
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
</StackPanel>