簡體   English   中英

嵌套用戶控件中定義的訪問命令 wpf

[英]access command defined in nested user controls wpf

我有一個從Window類派生的MainWindow 該窗口有一個ContentControl ,用於在其中承載用戶UserControl對象。 我正在嘗試創建一個按鈕,該按鈕將訪問在該嵌套UserControl定義的Command 唯一的技巧是我的用戶控件實際上也嵌套了更多的用戶控件,而且我實際上是要使用 Grand-child 命令而不僅僅是一個子控件。 像這樣的東西:

主窗口:

<metro:MetroWindow  x:Class="GrimshawRibbon.Revit.Wpf.MainWindow"
                    x:Name="win"
                    xmlns:metro="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
                    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                    xmlns:pmModel="clr-namespace:GrimshawRibbon.Revit.Management.ParametersManagerWPF.ViewModel"
                    xmlns:pmLocal="clr-namespace:GrimshawRibbon.Revit.Management.ParametersManagerWPF"
                    xmlns:local="clr-namespace:GrimshawRibbon.Revit.Wpf"
                    mc:Ignorable="d" Height="400" Width="600" ResizeMode="CanResizeWithGrip" 
                    Title="{Binding WindowTitle, Mode=OneWay, FallbackValue='GrimshawDT'}" BorderBrush="{DynamicResource AccentColorBrush}" BorderThickness="1"
                    WindowStartupLocation="CenterScreen" WindowTransitionsEnabled="False">
    <metro:MetroWindow.Resources>
        <ResourceDictionary>
            <DataTemplate DataType="{x:Type pmModel:pmViewModel}">
                <pmLocal:pmMain />
            </DataTemplate>
        </ResourceDictionary>
    </metro:MetroWindow.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height = "100" />
            <RowDefinition/>
        </Grid.RowDefinitions>

        <ContentControl x:Name="MainContentControl" Content="{Binding CurrentPageViewModel}" Margin="10" Grid.Row="1"/>
        <Button x:Name="btnOK" Content="OK" Margin="0,0,211,10" HorizontalAlignment="Right" Width="75" Height="36" VerticalAlignment="Bottom" Command="{Binding pmModel:pmViewModel.ApplyCommand, Mode=OneWay}" Grid.Row="1" metro:ButtonHelper.CornerRadius="0" metro:ControlsHelper.ContentCharacterCasing="Normal" BorderThickness="1" metro:ButtonHelper.PreserveTextCase="True"/>
    </Grid>
</metro:MetroWindow>

嵌套在 MainWindow 中的控件:

<UserControl x:Class="GrimshawRibbon.Revit.Management.ParametersManagerWPF.pmMain"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
             xmlns:ignore="http://www.ignore.com"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:GrimshawRibbon.Revit.Management.ParametersManagerWPF" 
             xmlns:viewModel="clr-namespace:GrimshawRibbon.Revit.Management.ParametersManagerWPF.ViewModel"
             xmlns:Custom="http://metro.mahapps.com/winfx/xaml/controls"
             mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="600">
    <UserControl.Resources>
        <ResourceDictionary>
            <DataTemplate DataType="{x:Type viewModel:pmSetParamToValueViewModel}">
                <local:pmSetParamToValueView />
            </DataTemplate>
            <DataTemplate DataType="{x:Type viewModel:pmCopyParamToParamViewModel}">
                <local:pmCopyParamToParamView />
            </DataTemplate>
            <DataTemplate DataType="{x:Type viewModel:pmCopyParamToParamSliceViewModel}">
                <local:pmCopyParamToParamSliceView />
            </DataTemplate>
            <DataTemplate DataType="{x:Type viewModel:pmCombineTwoSlicesViewModel}">
                <local:pmCombineTwoSlicesView />
            </DataTemplate>
        </ResourceDictionary>
    </UserControl.Resources>
    <Grid>
        <ComboBox x:Name="cbType" Margin="0,26,0,0" Height="22" VerticalAlignment="Top" ItemsSource="{Binding PageNames}" SelectedIndex="{Binding SelectedVMIndex}" Custom:TextBoxHelper.Watermark="Parameter Manager Functionality"/>
        <ContentControl x:Name="contentControl" Content="{Binding CurrentPageViewModel}" Margin="0,57,0,0"/>
        <Label x:Name="label" Content="Select Functionality:" HorizontalAlignment="Left" VerticalAlignment="Top" FontWeight="Bold"/>
    </Grid>
</UserControl>

這反過來有更多的嵌套控件,如下所示:

<UserControl x:Class="GrimshawRibbon.Revit.Management.ParametersManagerWPF.pmSetParamToValueView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:local="clr-namespace:GrimshawRibbon.Revit.Management.ParametersManagerWPF"
             xmlns:Custom="http://metro.mahapps.com/winfx/xaml/controls" 
             mc:Ignorable="d" d:DesignWidth="600" d:DesignHeight="200">
    <Grid>
        <ComboBox x:Name="cbCategories" HorizontalAlignment="Left" VerticalAlignment="Top" Width="180" ItemsSource="{Binding Categories}" DisplayMemberPath="Name" SelectedItem="{Binding SelectedCategory}" Custom:TextBoxHelper.Watermark="Category" Margin="0,26,0,0"/>
        <ComboBox x:Name="cbSourceParam" HorizontalAlignment="Left" Margin="0,57,0,0" Width="180" Height="22" VerticalAlignment="Top" ItemsSource="{Binding Parameters}" DisplayMemberPath="Name" SelectedItem="{Binding SelectedParameter}" Custom:TextBoxHelper.Watermark="Source Parameter"/>
        <TextBox x:Name="tbParamValue" Margin="185,57,0,0" Height="23" VerticalAlignment="Top" Text="{Binding Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Custom:TextBoxHelper.Watermark="Parameter Value"/>
        <Label x:Name="label" Content="Define Parameters:" HorizontalAlignment="Left" VerticalAlignment="Top" FontWeight="Bold"/>
    </Grid>
</UserControl>

現在,當在主窗口中點擊 OK 按鈕時,我希望第二個嵌套控件執行ApplyCommand 這是我的第二個用戶控件的視圖模型:

namespace GrimshawRibbon.Revit.Management.ParametersManagerWPF.ViewModel
{
    public class pmSetParamToValueViewModel : ViewModelBase
    {
        public pmModel model;
        public ObservableCollection<CategoryWrapper> Categories { get; private set; }
        public RelayCommand ApplyCommand { get; private set; }

        public pmSetParamToValueViewModel(Document doc)
        {
            this.model = new pmModel(doc);
            this.Categories = model.CollectCategories();
            SelectedCategory = Categories[0];
            this.ApplyCommand = new RelayCommand(this.Apply);
        }

        // logic for apply button
        private void Apply()
        {
            model.Apply(SelectedCategory.ID, SelectedParameter, null, null, Value, null, null, null, "Apply");
        }

        // logic for storing parameter value
        private string _value;
        public string Value
        {
            get { return _value; }
            set
            {
                if (_value == value) return;

                _value = value;
                RaisePropertyChanged(() => Value);
            }
        }

        // storage for source parameters
        private ObservableCollection<ParameterWrapper> _parameters;
        public ObservableCollection<ParameterWrapper> Parameters
        {
            get { return _parameters; }
            set
            {
                if (_parameters == value) return;

                _parameters = value;
                RaisePropertyChanged(() => Parameters);
                if (Parameters.Count > 0)
                {
                    SelectedParameter = Parameters[0];
                }
            }
        }

        // logic for selected category
        private CategoryWrapper _selectedCategory;
        public CategoryWrapper SelectedCategory
        {
            get { return _selectedCategory; }
            set
            {
                if (_selectedCategory == value) return;

                _selectedCategory = value;
                RaisePropertyChanged(() => SelectedCategory);
                Parameters = model.CollectParameters(SelectedCategory.ID, new string[] { "String", "Double", "Integer" });
            }
        }

        // logic for selected source parameter
        private ParameterWrapper _selectedParameter;
        public ParameterWrapper SelectedParameter
        {
            get { return _selectedParameter; }
            set
            {
                if (_selectedParameter == value) return;

                _selectedParameter = value;
                RaisePropertyChanged(() => SelectedParameter);
            }
        }
    }
}

有人能告訴我如何在主窗口中設置 OK 按鈕,以便能夠訪問在嵌套用戶控件中定義的命令。 或者,有沒有辦法設置一個 OK 按鈕來檢索給定嵌套用戶控件的當前狀態,以便我可以檢索它們的屬性並將它們用作我的ApplyCommand的參數?

干杯!

看起來好像您已經在主視圖模型中引用了子視圖模型,所以我相信這應該有效:

<Button x:Name="btnOK" Content="OK" Margin="0,0,211,10" HorizontalAlignment="Right" Width="75" Height="36" VerticalAlignment="Bottom" Command="{Binding CurrentPageViewModel.CurrentPageViewModel.ApplyCommand}" Grid.Row="1" metro:ButtonHelper.CornerRadius="0" metro:ControlsHelper.ContentCharacterCasing="Normal" BorderThickness="1" metro:ButtonHelper.PreserveTextCase="True"/>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM