一个用户控件对另一个用户控件的可见性

本文关键字:控件 用户 另一个 可见性 一个 | 更新日期: 2023-09-27 18:18:42

private void Button_Click(object sender, RoutedEventArgs e)
        {
            int selectedValue = (int)comboSelection.SelectedValue;
            if (selectedValue == 8)
            {
                EightTiles et = new EightTiles();
                this.Visibility = Visibility.Collapsed;
                et.Visibility = Visibility.Visible;
            }
        }

我的目标是当组合框选择等于8时,然后单击按钮,当前用户控件被折叠,下一个用户控件(EightTiles)可见。但我的问题是,当我点击按钮时,它显示一个空白页面,下一个用户控制页面没有显示,问题是什么,我如何解决它…由于

一个用户控件对另一个用户控件的可见性

请将这两个控件作为xaml的主要内容控件的内容,并根据第一个控件的触发来管理第二个控件的可见性。以下是我的建议:1. XAML:

<Window x:Class="SoDataGridProjectsHelpAttempt.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:soDataGridProjectsHelpAttempt="clr-namespace:SoDataGridProjectsHelpAttempt"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <ContentControl >
        <ContentControl.ContentTemplate>
            <DataTemplate>
                <Grid>
                    <soDataGridProjectsHelpAttempt:MainSubControl x:Name="MainSubControl" Visibility="Visible"/>
                    <soDataGridProjectsHelpAttempt:SubSubControl x:Name="SubSubControl" Visibility="Collapsed"/>
                </Grid>
                <DataTemplate.Triggers>
                    <Trigger Property="Control.Visibility" Value="Collapsed" SourceName="MainSubControl">
                        <Setter TargetName="SubSubControl" Property="Visibility" Value="Visible"></Setter>
                    </Trigger>
                </DataTemplate.Triggers>
            </DataTemplate>
        </ContentControl.ContentTemplate>
    </ContentControl>
</Grid>

2. MainSubControl:

<UserControl x:Class="SoDataGridProjectsHelpAttempt.MainSubControl"
         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:soDataGridProjectsHelpAttempt="clr-namespace:SoDataGridProjectsHelpAttempt"
         xmlns:collections="clr-namespace:System.Collections;assembly=mscorlib"
         xmlns:system="clr-namespace:System;assembly=mscorlib"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300" Visibility="{Binding IsControlVisible, Converter={StaticResource Bol2VisibilityConverter}, UpdateSourceTrigger=PropertyChanged}">
<UserControl.Resources>
    <x:Array Type="system:Int32" x:Key="DecimalsArray">
        <system:Int32>7</system:Int32>
        <system:Int32>5</system:Int32>
        <system:Int32>3</system:Int32>
        <system:Int32>8</system:Int32>
    </x:Array>
</UserControl.Resources>
<UserControl.DataContext>
    <soDataGridProjectsHelpAttempt:MainSubViewModel/>
</UserControl.DataContext>
<StackPanel>
    <ComboBox ItemsSource="{StaticResource DecimalsArray}" 
              Width="Auto" 
              SelectedItem="{Binding SelectedComboItem}"/>
    <Button Command="{Binding Command}">Press me!!!</Button>
</StackPanel>

3.MainSubControl ViewModel:

    public class MainSubViewModel : BaseObservableObject
{
    private int _selectedComboItem;
    private ICommand _command;
    private bool _isControlVisible;
    public MainSubViewModel()
    {
        IsControlVisible = true;
    }
    public ICommand Command
    {
        get { return _command ?? (_command = new RelayCommand(CommandMethod)); }
    }
    private void CommandMethod()
    {
        if (SelectedComboItem == 8)
            IsControlVisible = false;
    }
    public bool IsControlVisible
    {
        get { return _isControlVisible; }
        set
        {
            _isControlVisible = value;
            OnPropertyChanged();
        }
    }
    public int SelectedComboItem
    {
        get { return _selectedComboItem; }
        set
        {
            _selectedComboItem = value;
            OnPropertyChanged();
        }
    }
}

4。SecondSubControl:

<UserControl x:Class="SoDataGridProjectsHelpAttempt.SubSubControl"
         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" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid>
    <Image Source="2014_8_27_Bing_en-AU.jpg" Margin="50"></Image>
</Grid>

5. App.xaml(放入Application.Resources):

<BooleanToVisibilityConverter x:Key="Bol2VisibilityConverter" />

认为,

你的问题是et超出了作用域,所以在函数之后它不再存在了。你需要在"外部"的作用域中创建它,在这个作用域中它仍然存在于该函数之前和之后。

例如,如果有一个叫做Application的类那个类持有你的Button_click所附加的当前控件,你也需要在那里创建另一个控件。或者至少使用et变量,如果你不想创建EightTiles,直到你需要它。