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

时间:2015-10-22 06:19:00

标签: c# wpf xaml user-controls visibility

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然后单击按钮,当前用户控件被折叠并且下一个usercontrol(EightTiles)变得可见。 但我的问题是当我点击它显示一个空白页面的按钮,下一个用户控制页面没有显示,有什么问题,我该如何解决它.. 感谢

2 个答案:

答案 0 :(得分:0)

你的问题是et超出范围,所以在功能不再存在之后。你需要在一个范围内“创建”它,它在该函数之前和之后仍然存在。

因此,例如,如果有一个名为Application的类,并且该类保存了Button_click附加到的当前控件,则还需要在其中创建另一个控件。或者至少是et变量,如果你不想在需要之前创建EightTiles。

答案 1 :(得分:0)

将这些控件作为主要内容控件的内容发送到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" />

的问候,