所以我在这里看到过类似的问题,但一直没能指出这一点。我们有一个页面,其中包含一个UserControl,其中的一个StackPanel隐藏在onload上,并且父页面有一个按钮,当初始点击时我们想要隐藏它,需要在UserControl内部创建一个StackPanel(UserControl1.xaml中的stkSomePanel
) onload - 目前代码的大部分值为Visible
,所以我们可以看到它并尝试隐藏它 - 部分问题是知道将.Hidden
和.Visible
也放在哪里)。按钮文本必须从“编辑”更改为“保存”。当再次点击时,StackPanel的可见性需要切换回隐藏状态,文本回到“编辑”状态。WPF C# - 使用户控件中的堆栈面板可以从父母看到
应该是一个简单的概念,但不清楚如何绑定什么。我有一个父按钮,我试图用它来点击我想要隐藏的子按钮,但不知道我甚至需要子按钮。我已经测试了这种变化,我可以点击子按钮,它会更新子按钮的按钮文本以及子控件StackPanel的一次可见性,指示它运行ClickHereExecuted()
,但这只是一次性设置,而不是一个切换,如果我点击父按钮,什么都不会发生,这实际上是需要工作的。
到目前为止,MainWindow.xaml:
<Window x:Class="MyProject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="clr-namespace:MyProject"
xmlns:v="clr-namespace:MyProject.UserControls"
Title="MainWindow" Height="350" Width="525"
>
<StackPanel>
<v:GreatUserControl x:Name="UC1" />
<Button Content="{Binding ButtonContent}" Command="{Binding ClickHereCommand}"/>
</StackPanel>
</Window>
MainWindow.xaml.cs:
public partial class MainWindow : Window
{
public static RoutedCommand ClickHereCommand {get; set;}
public MainWindow()
{
InitializeComponent();
}
}
UserControl1.xaml:
<UserControl
x:Class="MyProject.GreatUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
<UserControl.Resources>
<BooleanToVisibiltyConverter x:Key="ConvBoolToVis"/>
</UserControl.Resources>
<Button Content="{Binding ButtonContent}" Command="{Binding ClickHereCommand}" />
<StackPanel x:Name="stkSomePanel" DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}" Visibility="{Binding vis, ElementName=UserControl1, Converter={StaticResource ConvBoolToVis}}">
</StackPanel>
</UserControl>
UserControl1.xaml.cs:
namespace MyProject.UserControls
{
public partial class UserControl1 : UserControl, INotifyPropertyChanged
{
public Visibility vis
{
get { return (Visibility)GetValue(VisibilityProperty); }
set { SetValue(VisibilityProperty, value); }
}
public static readonly DependencyProperty VisiblityProperty =
DependencyProperty.Register("vis", typeof(Visibility), typeof(UserControl1), new UIPropertyMetadata(Visibility.Visible));
public string ButtonContent
{
get { return (string)GetValue(ButtonContentProperty); }
set { SetValue(ButtonContentProperty, value); }
}
public static readonly DependencyProperty ButtonContentProperty =
DependencyProperty.Register("ButtonContent", typeof(string), typeof(UserControl1), new UIPropertyMetadata(string.Empty));
public RoutedCommand ClickHereCommand
{
get { return (RoutedCommand)GetValue(ClickHereCommandProperty); }
set { SetValue(ClickHereCommandProperty, value); }
}
public static readonly DependencyProperty ClickHereCommandProperty =
DependencyProperty.Register("ClickHereCommand", typeof(RoutedCommand), typeof(UserControl1), new UIPropertyMetadata(null));
public UserControl1()
{
InitializeComponent();
ClickHereCommand = new RoutedCommand();
CommandBindings.Add(new CommandBinding(ClickHereCommand, ClickHereExecuted));
ButtonContent = "Edit";
}
public void ClickHereExecuted(object sender, ExecutedRoutedEventArgs e)
{
ButtonContent = "Save";
vis = Visibility.Visible;
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string name)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
#endregion
}
}
如果它得到了你的正确,你想要在窗口中的按钮和在UC的StackPanel。当点击窗口中的按钮时,该StackPanel应该隐藏? – AnjumSKhan
@AnjumSKhan是的。 – vapcguy
@AnjumSKhan更准确地说,按钮会在UC中显示隐藏的StackPanel,并在再次单击时将其隐藏。文本将从“编辑”(默认)更改为“保存”,并显示StackPanel。当再次点击时,“保存”变为“编辑”并且StackPanel再次被隐藏。 – vapcguy