简体   繁体   English

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

[英]Visibility of one user control to another user control

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;
            }
        }

My target is when the combo box selection is equal to 8 then click the button, current usercontrol get collapsed and next usercontrol(EightTiles) get visible. 我的目标是当组合框选择等于8时,单击按钮,当前用户控件折叠起来,而下一个usercontrol(EightTiles)可见。 But my problem is when i click the button it shows a blank page, next user control page doesn't showed, what is the problem and how i do to solve it.. Thanks 但是我的问题是,当我单击该按钮时,它显示一个空白页面,下一个用户控制页面没有显示,这是什么问题,我该如何解决..谢谢

your problem is that et goes out of scope, so after the function is doesn't exist anymore. 您的问题是et超出范围,因此该功能不再存在。 you need to create it "outside" in a scope where it will still exist before and after that function. 您需要在该函数之前和之后仍然存在的范围内“外部”创建它。

So for example if there's a class called Application and that class holds the current control that your Button_click is attached to, you need to create the other control there too. 因此,例如,如果有一个名为Application的类,并且该类包含Button_click附加到的当前控件,那么您也需要在该控件中创建另一个控件。 Or at least the et variable if you don't want to create the EightTiles until you need it. 如果您不想在需要之前创建AugTiles,则至少可以使用et变量。

Plase both these controls as content of a main content control to xaml, and manage the second control visibility based on trigger of the first control. 将这两个控件作为主要内容控件的内容放入xaml,并基于第一个控件的触发来管理第二个控件可见性。 Here what I can suggest you: 1. 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: 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: 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: 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 (put into Application.Resources): 5. App.xaml(放入Application.Resources):

<BooleanToVisibilityConverter x:Key="Bol2VisibilityConverter" />

regards, 问候,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM