You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在XAML中禁用嵌入为本地视图的子视图内的按钮?

需求描述

我有一个大型XAML文件FromView.xaml,它创建了一个包含多个按钮、标签等元素的StackPanel视图,作为主窗口的标签页之一显示。另有汇总视图ToView.xaml作为标签页,用于展示各标签页的特定内容。我已通过以下代码将FromView嵌入ToView中:

<!-- ToView.xaml -->
<Canvas MinHeight="300" MinWidth="750" Margin="2">
    <local:FromView Height="300" Width="750" IsSimplifiedView="True"/>
</Canvas>

现在需要从ToView中控制FromView内按钮的Visibility属性,隐藏这些按钮。FromView中的按钮代码片段如下:

<StackPanel Grid.Column="2" Width="280" HorizontalAlignment="Left" >
    <Grid Background="{StaticResource QualificationBackground}">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="120"/>
            <ColumnDefinition Width="80"/>
            <ColumnDefinition Width="20*"/>
            <ColumnDefinition Width="40"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>

        <!--some stuff-->

        <Label Content="SomeContent"  VerticalAlignment="Center" HorizontalAlignment="Right" Grid.Row="6" Grid.Column="0"/>
        <Button Grid.Row="6" Grid.Column="2" Grid.ColumnSpan="2" Width="60" Height="Auto" HorizontalAlignment="Left" Content="Save"/>

        <!--some stuff-->
    </Grid>
</StackPanel>
实现方法

方法1:基于已有的IsSimplifiedView依赖属性绑定

你已经在FromView上设置了IsSimplifiedView="True",可以直接利用这个属性控制按钮可见性:

  1. 确保FromView.xaml.cs中IsSimplifiedView是依赖属性(未定义则添加):
public static readonly DependencyProperty IsSimplifiedViewProperty =
    DependencyProperty.Register("IsSimplifiedView", typeof(bool), typeof(FromView), new PropertyMetadata(false));

public bool IsSimplifiedView
{
    get { return (bool)GetValue(IsSimplifiedViewProperty); }
    set { SetValue(IsSimplifiedViewProperty, value); }
}
  1. 在FromView.xaml中添加转换器资源(全局未定义时):
<UserControl.Resources>
    <BooleanToVisibilityConverter x:Key="BoolToVis"/>
</UserControl.Resources>
  1. 修改按钮的Visibility绑定,将IsSimplifiedView转换为隐藏状态:
<Button 
    Grid.Row="6" Grid.Column="2" Grid.ColumnSpan="2" 
    Width="60" Height="Auto" HorizontalAlignment="Left" 
    Content="Save"
    Visibility="{Binding IsSimplifiedView, RelativeSource={RelativeSource AncestorType=local:FromView}, Converter={StaticResource BoolToVis}, ConverterParameter=Inverse}"/>

注:如果转换器不支持ConverterParameter=Inverse,可以自定义一个反向布尔转可见性的转换器,或者用多值绑定实现取反逻辑。

方法2:使用样式触发器

无需转换器,直接通过样式触发器控制按钮可见性:

<Button 
    Grid.Row="6" Grid.Column="2" Grid.ColumnSpan="2" 
    Width="60" Height="Auto" HorizontalAlignment="Left" 
    Content="Save">
    <Button.Style>
        <Style TargetType="Button">
            <Setter Property="Visibility" Value="Visible"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsSimplifiedView, RelativeSource={RelativeSource AncestorType=local:FromView}}" Value="True">
                    <Setter Property="Visibility" Value="Collapsed"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>

方法3:MVVM模式下的视图模型绑定

如果项目采用MVVM,建议通过共享视图模型实现状态同步:

  1. 在共享视图模型中添加属性(需实现INotifyPropertyChanged):
private bool _isSimplifiedView;
public bool IsSimplifiedView
{
    get => _isSimplifiedView;
    set => SetProperty(ref _isSimplifiedView, value);
}
  1. 在ToView.xaml中绑定FromView的IsSimplifiedView到视图模型:
<local:FromView Height="300" Width="750" IsSimplifiedView="{Binding IsSimplifiedView}"/>
  1. 在FromView的按钮中绑定视图模型属性:
<Button 
    Grid.Row="6" Grid.Column="2" Grid.ColumnSpan="2" 
    Width="60" Height="Auto" HorizontalAlignment="Left" 
    Content="Save"
    Visibility="{Binding IsSimplifiedView, Converter={StaticResource BoolToVis}, ConverterParameter=Inverse}"/>

内容的提问来源于stack exchange,提问作者İpek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 07:35:00