所以我在這裏看到過類似的問題,但一直沒能指出這一點。我們有一個頁面,其中包含一個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