c# - 如何通过上下文菜单控制动态生成的控件的可见性?

标签 c# wpf xaml dynamic mvvm

我有一个 ItemsControl,它绑定(bind)到我的 ViewModel 中的属性列表,并使用 DataTemplate,根据属性的类型自动生成适当的控件。

这是 ItemsControl 的代码:

<ItemsControl Grid.Column="0" ItemsSource="{Binding Properties}" >
    <ItemsControl.Resources>
        <DataTemplate x:Key="StringTemplate" DataType="{x:Type ui:ControlStringData}" >
            <xctk:WatermarkTextBox Text="{Binding Path=Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                        Watermark="{Binding Path=Caption, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                        Margin="5,5,5,5" />
        </DataTemplate>
        <DataTemplate x:Key="DateTimeTemplate" DataType="{x:Type ui:ControlDateTimeData}">
            <xctk:DateTimePicker Value="{Binding Path=Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                        Watermark="{Binding Path=Caption, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                        Margin="5,5,5,5" />
        </DataTemplate>
        <DataTemplate x:Key="DefaultTemplate" />
    </ItemsControl.Resources>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <ContentControl
                    x:Name="MyContentControl"
                    Content="{Binding}"
                    ContentTemplate="{StaticResource DefaultTemplate}"/>
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding Path=Type}" Value="DateTime">
                    <Setter TargetName="MyContentControl" Property="ContentTemplate"
                                Value="{StaticResource DateTimeTemplate}" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=Type}" Value="String">
                    <Setter TargetName="MyContentControl" Property="ContentTemplate"
                                Value="{StaticResource StringTemplate}" />
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <UniformGrid />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>

现在有了生成的控件,我想创建一个 ContextMenu,其中填充了 Caption 属性和一个复选框,其中 ContextMenu 中的每个项目都绑定(bind)到相应控件的可见性。

我目前的代码是一个按钮,它将打开一个上下文菜单,如下所示:

<Button Margin="5,5,5,5" Grid.Column="1" Grid.Row="0" Height="16" Width="16">
    <Button.Style>
        <Style TargetType="{x:Type Button}">
            <Style.Triggers>
                <EventTrigger RoutedEvent="Click">
                    <EventTrigger.Actions>
                        <BeginStoryboard>
                            <Storyboard>
                                <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="ContextMenu.IsOpen">
                                    <DiscreteBooleanKeyFrame KeyTime="0:0:0" Value="True"/>
                                </BooleanAnimationUsingKeyFrames>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger.Actions>
                </EventTrigger>
            </Style.Triggers>
            <Setter Property="ContextMenu">
                <Setter.Value>
                    <ContextMenu />

                </Setter.Value>
            </Setter>
        </Style>
    </Button.Style>
</Button>

如何创建上下文菜单并将选项限制在可见性范围内?我被卡住了,因为我不确定如何正确设置绑定(bind)来执行此操作。

更多详情:

所以在上面的 ItemsControl 中,它绑定(bind)到一个名为“Properties”的属性列表,其中每个元素实现:

public interface IControlData
{
    string Type { get; set; }
    string Name { get; set; }
    string Caption { get; set; }
    bool Visibility { get; set; }
}

基于类型字符串,我在上面使用数据触发器为该类型生成正确的控件,并将它们放置在模板化为 UniformGrid 的 ItemsControl 中。

现在我想要一个按钮,单击该按钮会显示 ItemsControl 中每个控件的名称列表,并且每个条目都是可检查的,其中可检查状态对应于 ItemsControl 中控件的可见性。

示例:如果 ItemsControl 网格是这样的:

+----------+----------+
| TextBox1 | TextBox2 |
+----------+----------+
| TextBox3 | TextBox4 |
+----------+----------+

然后当我点击按钮时显示的上下文菜单将是:

+---+----------+
| ✓ | TextBox1 |
| ✓ | TextBox2 |
| ✓ | TextBox3 |
| ✓ | TextBox4 |
+---+----------+

如果我单击 ContextMenu 中的一个条目,例如 TextBox4,它会将 TextBox4 的可见性设置为 false,并且项目控件将如下所示:

+----------+----------+
| TextBox1 | TextBox2 |
+----------+----------+
| TextBox3 |          |
+----------+----------+

上下文菜单将显示为:

+---+----------+
| ✓ | TextBox1 |
| ✓ | TextBox2 |
| ✓ | TextBox3 |
|   | TextBox4 |
+---+----------+

最佳答案

使用 DataTemplateSelector。

此类旨在处理基于状态的各种数据模板的选项。

http://msdn.microsoft.com/en-us/library/system.windows.controls.datatemplateselector(v=vs.110).aspx

关于c# - 如何通过上下文菜单控制动态生成的控件的可见性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25250415/

相关文章:

c# - 混淆变量被覆盖

c# - WPF 中 Canvas 的大小

wpf - 复制不适用于数据网格模板列?

C#:使用鼠标右键拖放

wpf - 如何在 XAML 中使用文字大括号 "{"?

c# - (WPF 的)窗口可以知道它是否是由 ShowDialog() 打开的吗?

c# - 列出 Windows Azure Blob 存储容器中的 EMPTY 目录

c# - 我的 NET Core Web API Controller 出现“找不到页面”错误

c# - XAML - 获取 ListView 以使用网格单元格的全高

wpf - 将文本 block 绑定(bind)到纯 xaml 中的当前列表框项目