我目前有两个用户控件,UC1和UC2。我有一个主窗口。在应用程序开始时,用户可以看到UC1。当用户点击UC1中的按钮时,UC1应该消失,UC2应该显示在同一个窗口中。我尝试了以下方法:
MainWindow.xaml
<Window x:Class="Test.MainWindow"
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" xmlns:my="clr-namespace:Test" WindowState="Maximized">
<Grid>
<my:UC1 />
<my:UC2 Visibility="Collapsed"/>
</Grid>
最初两个UC都是同时可见的,所以我让UC2崩溃了。现在我想在单击UC1中的按钮时再次使其可见。我无法从UC1访问UC2的可见性。
UC1.xaml.cs
private void button2_click(object sender, MouseButtonEventArgs e)
{
this.Visibility = System.Windows.Visibility.Collapsed;
// What to write here?
}
修改
对于2个控制器UC1和UC2,使用Tag属性可以正常工作。现在我添加另一个控制器UC3,只有在单击一个不同的按钮后才可见。
所以,最后我有UC1,UC2和UC3,其中只有UC1可见。 UC1有两个按钮,只需点击第一个按钮,只有UC2可见,点击第二个按钮,只有UC3可见。
我无法绑定多个标签。有多个标签转换器吗?
答案 0 :(得分:2)
<Grid>
<my:UC1 Name="UC1" />
<my:UC2 Name="UC2" Visibility="Collapsed"/>
</Grid>
private void button2_click(object sender, MouseButtonEventArgs e)
{
this.UC1.Visibility = System.Windows.Visibility.Collapsed;
this.UC2.Visibility = System.Windows.Visibility.Visible;
}
刚刚意识到您的按钮位于UC1中。有很多方法可以做到这一点,但这可能是代码最少的一种方式。
<Grid>
<my:UC2 Name="UC2" Visibility="Collapsed" />
<my:UC1 Tag="{Binding ElementName=UC2}" />
</Grid>
private void button2_click(object sender, MouseButtonEventArgs e)
{
this.Visibility = System.Windows.Visibility.Collapsed;
var uc2 = this.Tag as UC2;
if (uc2 != null)
uc2.Visibility = System.Windows.Visibility.Visible;
}
这种方法最短,但其他人可能更难弄清楚你写的是什么。
如果你想要一个好的方法,可以在MainWindow
,绑定UC2
的{{1}}到Visibility
,然后使用转换器。
UC1
答案 1 :(得分:1)
编辑: 发布整个代码以使答案更清晰。我也没有任何例外。
MainWindow.xaml:
<Window x:Class="WpfApplication4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:h="clr-namespace:WpfApplication4"
Title="MainWindow"
Width="525"
Height="350">
<Grid>
<h:UC1 x:Name="Uc1" />
<h:UC2 x:Name="Uc2" Visibility="Hidden" />
</Grid>
</Window>
MainWindow.xaml.cs:
using System.Windows;
namespace WpfApplication4
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}
UC1.xaml:
<UserControl x:Class="WpfApplication4.UC1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
d:DesignHeight="300"
d:DesignWidth="300"
mc:Ignorable="d">
<Grid>
<Ellipse Width="100"
Height="100"
Margin="65,113,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Fill="CornflowerBlue"
Stroke="Black" />
<Button Width="75"
Margin="177,239,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Click="ButtonBase_OnClick"
Content="ABCD" />
</Grid>
</UserControl>
UC1.xaml.cs:
using System.Windows;
using System.Windows.Controls;
namespace WpfApplication4
{
/// <summary>
/// Interaction logic for UC_.xaml
/// </summary>
public partial class UC1 : UserControl
{
public UC1()
{
InitializeComponent();
}
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
Window parentWindow = Application.Current.MainWindow;
if (parentWindow.GetType() == typeof(MainWindow))
{
(parentWindow as MainWindow).Uc1.Visibility = Visibility.Collapsed;
(parentWindow as MainWindow).Uc2.Visibility = Visibility.Visible;
}
}
}
}
UC2.xaml:
<UserControl x:Class="WpfApplication4.UC2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
d:DesignHeight="300"
d:DesignWidth="300"
mc:Ignorable="d">
<Grid>
<Ellipse Width="100"
Height="100"
Margin="169,22,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Fill="Orange"
Stroke="Black" />
</Grid>
</UserControl>
UC2.xaml.cs:
using System.Windows.Controls;
namespace WpfApplication4
{
/// <summary>
/// Interaction logic for UC2.xaml
/// </summary>
public partial class UC2 : UserControl
{
public UC2()
{
InitializeComponent();
}
}
}
答案 2 :(得分:1)
第二个UC取决于第一个UC,因此您需要Binding
。
将IValueConverter
写入切换Visibility
,以便两个UC之间不存在依赖关系。
<Window.Resources>
<local:ToggleVisConv x:Key="ConvKey"/>
</Window.Resources>
<Grid>
<local:UserControl1 x:Name="UC1"/>
<local:UserControl2 Visibility="{Binding Visibility, ElementName=UC1, Converter={StaticResource ConvKey}}"/>
</Grid>
转换器代码:
public class ToggleVisConv : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Visibility v = (Visibility)value;
if (v == Visibility.Collapsed)
return Visibility.Visible;
return Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
答案 3 :(得分:0)
我使用依赖属性
的Xaml:
<StackPanel>
<vm:UC1 GetUserControl2="{Binding ElementName=usrUC2}" x:Name="usrUC1"/>
<vm:UC2 x:Name="usrUC2"/>
</StackPanel>
类别:
public partial class UC1 : UserControl
{
public UC1()
{
InitializeComponent();
}
public static readonly DependencyProperty GetUserControlProperty =
DependencyProperty.Register("GetUserControl2", typeof(UserControl),
typeof(UC1), null);
public UserControl GetUserControl2
{
get
{
return (UserControl)GetValue(GetUserControlProperty);
}
set
{
SetValue(GetUserControlProperty, value);
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
GetUserControl2.Visibility = Visibility.Collapsed;
}
}