代码之家  ›  专栏  ›  技术社区  ›  Homam

将按钮的IsEnabled属性与数据网格中的选定项绑定在一起的最佳实践是什么?

  •  0
  • Homam  · 技术社区  · 14 年前

    这些按钮将根据网格中选定项的属性隐藏或禁用。

    实施这种情况的最佳实践是什么?

    我可以在SelectedItemChange中手动实现它,如下所示:

    void myGrid_selectedItemChanged(sender, args..)
    {
        cancelButton.IsEnabled = SelectedItem.IsNew ; // just an example
    }
    

    此方法的问题是,当删除选定项时,不会触发此事件。

    或者在XAML中,将依赖项属性与控件绑定:

    <!--SelectedItemIsNew is a property in the form and binding it with IsEnabled... -->
    <Button x:Name="cancelButton" IsEnabled="{Binding SelectedItemIsNew ...}"/>
    

    这里的问题是,我必须为属性分配一个值以通知绑定控件,或者换句话说,这里的代码是什么来更改属性的值?

    另一个选择!

    3 回复  |  直到 13 年前
        1
  •  0
  •   Singleton    14 年前

    嘿,我以前也遇到过同样的情况,当我在SL/WPF中用网格或列表绑定一些数据时,我总是这样做,因为这很容易理解。为什么不试着将coustom列表绑定到代码中呢??例如。。像say String hyperlinknaviation那样获取和归档

    当您从服务获取值,然后在将列表分配给网格之前,迭代您的所有项并为HyperLinkNaviagtion分配一些值,如

     if(SomeCondition)
        { 
          // navigaet to google
              HyperLinkNaviagtion ="www.google.com";
        } 
     else 
       { 
         // navigaet to yahoo
           HyperLinkNaviagtion ="www.yahoo.com";
       }
    

    在Xaml中,执行以下操作

    NavigateUri=“{绑定超链接导航}”

    是唐,你几乎可以控制这样的一切

        2
  •  1
  •   Vlad    14 年前

    必须正确:

    <Button x:Name="cancelButton" IsEnabled="{Binding IsNew, Source=SelectedItem}"/>
    

    问题是,你的SelectedItem在哪里?它是DataContext的属性,还是控件的属性(或控件中某个列表的属性)?


    对于控件属性,应执行以下操作:

    <Button x:Name="cancelButton"
            IsEnabled="{Binding SelectedItem.IsNew,
                                RelativeSource={RelativeSource FindAncestor, AncestorType=local:MyControl}}"/>
    
        3
  •  1
  •   Tim Cooper    13 年前

    视图模型代码:

    public class ParentViewModel : ViewModel
    {
        private readonly ICommand cancelCommand;
        private readonly ICollection<ChildViewModel> children;
        private ChildViewModel selectedChild;
    
        public ParentViewModel()
        {
            this.cancelCommand = new DelegateCommand(this.OnCancel, this.CanCancel);
            this.children = new ObservableCollection<ChildViewModel>();
        }
    
        public ICommand CancelCommand
        {
            get { return this.cancelCommand; }
        }
    
        public ICollection<ChildViewModel> Children
        {
            get { return this.children; }
        }
    
        public ChildViewModel SelectedChild
        {
            get { return this.selectedChild; }
            set
            {
                if (this.selectedChild != value)
                {
                    this.selectedChild = value;
                    this.OnPropertyChanged(() => this.SelectedChild);
                }
            }
        }
    
        private void OnCancel(object parameter)
        {
            // cancel logic here
        }
    
        private bool CanCancel(object parameter)
        {
            // can only cancel if there's a child selected
            return this.SelectedChild != null;
        }
    }
    

    XAML:

    <GridView ItemsSource="{Binding Children}" SelectedItem="{Binding SelectedChild}">
        ...
    </GridView>
    
    ...
    
    <Button Command="{Binding CancelCommand}" Content="Cancel"/>