WPF Material Design Update窗口关闭对话框的上下文?

时间:2018-03-06 09:57:01

标签: c# wpf material-design

我正在为我的WPF应用程序(https://github.com/ButchersBoy/MaterialDesignInXamlToolkit)实现MaterialDesign。我实现Snackbar菜单完全相同,我有几个Dialogs来捕获用户输入。

为每个窗口设置上下文,以便在加载时正确地显示在我的零食栏中。当我点击小吃栏项目时,它不会更新上下文。我不知道该怎么做

当对话框关闭(关闭事件)时,我想触发一个动作来更新用户的上下文,以查看具有最新值的最新网格。

这是我的ContentItem类

public class ContentItem : INotifyPropertyChanged
{
    private string _name;
    private object _content;
    private Thickness _marginRequirement = new Thickness(16);

    public ContentItem(string name, object content)
    {
        _name = name;
        Content = content;
    }

    public string Name
    {
        get => _name;
        set => this.MutateVerbose(ref _name, value, RaisePropertyChanged());
    }

    public object Content
    {
        get => _content;
        set => this.MutateVerbose(ref _content, value, RaisePropertyChanged());
    }

    public Thickness MarginRequirement
    {
        get => _marginRequirement;
        set => this.MutateVerbose(ref _marginRequirement, value, RaisePropertyChanged());
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private Action<PropertyChangedEventArgs> RaisePropertyChanged()
    {
        return args => PropertyChanged?.Invoke(this, args);
    }
}

这是我的MainWindowViewModel - 我在窗口实例化时添加了上下文,这是我想在快餐栏点击或对话框关闭事件上更新的上下文

public class MainWindowViewModel
{
    public MainWindowViewModel(ISnackbarMessageQueue snackbarMessageQueue)
    {
        if (snackbarMessageQueue == null) throw new ArgumentNullException(nameof(snackbarMessageQueue));

        ContentItems = new[]
        {
            new ContentItem("Sales", new SalesWindow()),
            new ContentItem("Stock Reeived", new StockWindow()),
            new ContentItem("Petty Cash", new PettyCashWindow()),
            new ContentItem("Banked", new BankedWindow())
        };
    }

    public ContentItem[] ContentItems { get; }
}

这是应用程序打开时调用的方式(MainWindow)

 InitializeComponent();
        Task.Factory.StartNew(() =>
        {
            Thread.Sleep(2500);
        }).ContinueWith(t =>
        {
            //note you can use the message queue from any thread, but just for the demo here we 
            //need to get the message queue from the snackbar, so need to be on the dispatcher
            MainSnackbar.MessageQueue.Enqueue("Welcome to DirectS");
        }, TaskScheduler.FromCurrentSynchronizationContext());

        //Set DataContext
        DataContext = new MainWindowViewModel(MainSnackbar.MessageQueue);

简而言之,我需要在单击“ContentItem”时以及在触发Dialog关闭事件时更新窗口的上下文.....我该怎么做?

1 个答案:

答案 0 :(得分:0)

在材料设计中,与演示一样,菜单面板是一个列表框。创建一个在对话框关闭事件或面板单击事件“UIElement_OnPreviewMouseLeftButtonUp”之后触发的方法,如下例所示。

<ListBox x:Name="DemoItemsListBox" Margin="0 16 0 16" SelectedIndex="0"                         
                         ItemsSource="{Binding ContentItems}"
                         PreviewMouseLeftButtonUp="UIElement_OnPreviewMouseLeftButtonUp">

在此之后抓取列表框项源DemoItems(与材质设计示例一样)循环遍历数组,找到要按名称更新的窗口并为内容项实例化一个新窗口。

private void UIElement_OnPreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        //until we had a StaysOpen glag to Drawer, this will help with scroll bars
        var dependencyObject = Mouse.Captured as DependencyObject;
        while (dependencyObject != null)
        {
            if (dependencyObject is ScrollBar) return;
            dependencyObject = VisualTreeHelper.GetParent(dependencyObject);
        }

        MenuToggleButton.IsChecked = false;

        var listBox = sender as ListBox;
        UpdateContentItems.Set(listBox);
    }

这是我的静态类,以帮助更新窗口上下文

public static class UpdateContentItems
{
    public static void Set(ListBox listBox)
    {
        var items = listBox.ItemsSource;
        foreach (var item in items)
        {
            var contentItem = item as ContentItem;
            if (contentItem.Name == "Banked")
            {
                contentItem.Content = new BankedWindow();
            }
        }
    }
}

触发了属性更改事件,您的窗口现在将是最新的。

可以将其清理一下,但希望这会有所帮助。