English 中文(简体)
在妇女论坛中,司令部如何能提高其他调查单位的知名度?
原标题:In WPF, how can a Command s CanExecute method gain visibility of other UI elements?

我先把世界森林论坛用于指挥部门,但我会再次使用。 有一个守则的例子后,我设立了一个单独的固定指挥组,以掌握我的所有指挥,我也这样做。

public static class Commands
{
    public static RoutedUICommand OpenDocument { get; set; }

    static Commands()
    {
        OpenDocument = new RoutedUICommand("Open Document", "OpenDocument", typeof(Commands));
    }

    public static void BindCommands(Window window)
    {
        window.CommandBindings.Add(new CommandBinding(OpenDocument, OpenDocument_Executed, OpenDocument_CanExecute));
    }

    private static void OpenDocument_CanExecute(object sender, CanExecuteRoutedEventArgs e)
    {
        // Should be set to true if an item is selected in the datagrid.
    }

    private static void OpenDocument_Executed(object sender, ExecutedRoutedEventArgs e)
    {

    }
}

My problem is that although the command is going to be bound to a Button control in MainWindow.xaml, the OpenDocument_CanExecute method needs to look at a DataGrid in MainWindow.xaml to see if an item is selected.

我怎么能够把事情连起来,使这种方法能够看到数据单。

在Kens的答复(又一次!)的启发下,我提出了以下内容,这完全奏效。

MainWindow.xaml.cs

public partial class MainWindow
{
    public MainWindow()
    {
        InitializeComponent();

        Loaded += delegate
        {
            DataContext = ViewModel.Current;
            Commands.BindCommands(this);
        };
    }
}

www.un.org/spanish/ecosoc ViewModel.cs

public class ViewModel
{
    private static ViewModel _current;
    public static ViewModel Current
    {
        get { return _current ?? (_current = new ViewModel()); }
        set { _current = value; }
    }

    public object SelectedItem { get; set; }
}

<>Commands.cs

public static class Commands
{
    public static RoutedUICommand OpenDocument { get; set; }

    static Commands()
    {
        OpenDocument = new RoutedUICommand("Open Document", "OpenDocument", typeof(Commands));
    }

    public static void BindCommands(Window window)
    {
        window.CommandBindings.Add(new CommandBinding(OpenDocument, OpenDocument_Executed, OpenDocument_CanExecute));
    }

    private static void OpenDocument_CanExecute(object sender, CanExecuteRoutedEventArgs e)
    {
        e.CanExecute = ViewModel.Current.SelectedItem != null;
    }

    private static void OpenDocument_Executed(object sender, ExecutedRoutedEventArgs e)
    {

    }
}
最佳回答

www.un.org/chinese/ga

class ViewModel : INotifyPropertyChanged {
    class OpenDocumentCommand : ICommand {
        public bool CanExecute(object parameter) {
            return ViewModel.ItemIsSelected;
        }
        public OpenDocumentCommand(ViewModel viewModel) {
            viewModel.PropertyChanged += (s, e) => {
                if ("ItemIsSelected" == e.PropertyName) {
                    RaiseCanExecuteChanged();
                }
            };
        }
    }

    private bool _ItemIsSelected;

    public bool ItemIsSelected {
        get { return _ItemIsSelected; }
        set {
            if (value == _ItemIsSelected) return;
            _ItemIsSelected = value;
            RaisePropertyChanged("ItemIsSelected");
        }
    }

    public ICommand OpenDocument { 
        get { return new OpenDocumentCommand(this); } 
    }
}

显然,我全心全意 left。 但这种模式过去对我来说是好的。

问题回答

如果你把指挥系统紧凑起来,为什么甚至实施指挥? 仅对数据网做出答复。 选择和编码假定发生的情况。

否则,将它列入《观点》。 The ViewModel是否对它进行了监测,并在CanExe一案成立时进行评估。

<><>Edit>>

而另一方面,你可以把参数传给你的指挥以及Exe() & CanExe()方法。

//where T is the type you want to operate on
public static RoutedUICommand<T> OpenDocument { get; set; }

如果你正在采用微型信贷额度解决办法,那将是实施一个出版物/用户汇总的完美时机,从而可以相互控制“alk”。 背后,数据网将公布一项活动,即公开文件。 随后的管制可以支持这一活动,并对公开文件呼吁作出反应。 公布/订阅模式使数据网和控制权的严格合并。 对事件汇总器进行一些搜索,我认为你会走。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签