一个用户控件对另一个用户控件的可见性
Posted
技术标签:
【中文标题】一个用户控件对另一个用户控件的可见性【英文标题】:Visibility of one user control to another user control 【发布时间】:2016-01-21 08:20:39 【问题描述】: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)变得可见。 但是我的问题是当我单击按钮时它显示一个空白页面,下一个用户控制页面没有显示,问题是什么以及我如何解决它.. 谢谢
【问题讨论】:
【参考方案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:
<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。第二子控件:
<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" />
问候,
【讨论】:
【参考方案2】:你的问题是 et 超出了范围,所以在函数 is 之后不再存在。您需要在该函数之前和之后仍然存在的范围内“外部”创建它。
因此,例如,如果有一个名为 Application 的类并且该类包含您的 Button_click 所附加到的当前控件,那么您也需要在那里创建另一个控件。或者至少是 et 变量,如果您不想在需要之前创建 EightTiles。
【讨论】:
以上是关于一个用户控件对另一个用户控件的可见性的主要内容,如果未能解决你的问题,请参考以下文章