无法访问父级的datacontext

时间:2015-05-18 10:51:05

标签: c# windows-phone-8 binding windows-phone-8.1 datacontext

基本上我拥有的是带有ContextMenu的ListBox

 <ListBox  Margin="2,0,0,0"  Grid.Row="1" ItemsSource="{Binding MyCollection}">
        <ListBox.ItemTemplate>
                <DataTemplate>
                    <Button  Style="{StaticResource NoVisualButton }" Tag="{Binding ID}" Width="430" toolkit:TiltEffect.IsTiltEnabled="True"  Margin="0,0,0,12" Click="OnSelectWorkOutItemClick">
                        <StackPanel>
                            <toolkit:ContextMenuService.ContextMenu>
                                <toolkit:ContextMenu>
                                    <toolkit:MenuItem Header="delete" Tag="{Binding ID}" Click="onContextMenuDeleteItemClick" IsEnabled="{Binding IsDeleteOptionEnable, ElementName=LayoutRoot}"/>
                                    <toolkit:MenuItem Header="edit"  Tag="{Binding ID}" Click="onContextMenuItemEditClick" />
                                </toolkit:ContextMenu>
                            </toolkit:ContextMenuService.ContextMenu>
                            ...
                        </StackPanel>
                    </Button>
                </DataTemplate>
       </ListBox.ItemTemplate>
 </ListBox>

因此,如果MyCollection只有一个项目,我必须禁用删除MenuItem。

我的模特有一个属性

   public bool IsDeleteOptionEnable
       {             
           get
           {
               return MyCollection.Count() >= 2;
           }
       }

在我正在设置DataContext的页面中:

   protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            if (IsDataLoaded)
            {
                this.DataContext =MyModel;
            }           
        }

列表框已经填充,但我无法禁用&#34;删除&#34;菜单项。我做错了什么?

3 个答案:

答案 0 :(得分:2)

由于IsDeleteOptionEnable是常规属性,因此在更改属性时,您的视图不会收到通知。选项将在您的模型中实现INotifyPropertyChanged(实际上应该是MVVM模式中的ViewModel),并在集合中的项目发生更改时调用PropertyChanged事件。

class YourModel : INotifyPropertyChanged {
    public event PropertyChangedEventHandler PropertyChanged;
    ..
    ..
    public YourModel() {
        this.MyCollection = ...;
        this.MyCollection.CollectionChanged += MyCollection_CollectionChanged;
    }
    public bool IsDeleteOptionEnable {             
       get {
           return MyCollection.Count() >= 2;
        }
    }
    private void MyCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) {
         this.OnPropertyChanged("IsDeleteOptionEnable");
    }
    private void OnPropertyChanged(string name = null) {
         if (this.PropertyChanged != null) {
            PropertyChangedEventArgs ea = new PropertyChangedEventArgs(name);
            this.PropertyChanged(this, ea);
         }
     }
}

现在,当项目被删除或添加到集合中时,模型会引发PropertyChanged事件,以便视图知道IsDeleteOptionEnable属性(实际上可能)已更改,并且按钮的已启用状态会更新。 / p>

答案 1 :(得分:1)

尝试

  IsEnabled="{Binding DataContext.IsDeleteOptionEnable, ElementName=LayoutRoot}"

答案 2 :(得分:0)

作为DataSource,您需要使用ObservableCollection。然后,您需要在包含绑定INotifyPropertyChanged的类中实现Property - 接口。

示例类:

// Example of binded object
public class MyItem: INotifyPropertyChanged {

   // Binded Property
   private String itemIsVisible = "Yes";
   public String ItemIsVisible{
      get { return itemIsVisible; }
      set {
         itemIsVisible = value;

         // This ensures the updating
         OnPropertyChanged("ItemIsVisible");
      }
   }

   protected void OnPropertyChanged(string name) {
      PropertyChangedEventHandler handler = PropertyChanged;
      if (handler != null) {
         handler(this, new PropertyChangedEventArgs(name));
      }
   }
}

示例XAML:

<TextBlock Text="{Binding ItemIsVisible}" />