WPF C# - 在父级中显示用户控件内的堆栈面板

时间:2016-09-20 01:04:04

标签: wpf button binding user-controls parent-child

所以我在这里看到了类似的问题,但是还没有能够解决这个问题。我们有一个包含UserControl的页面,其中的StackPanel被隐藏在onload上,而父对象有一个按钮需要在最初点击的UserControl内部显示StackPanel(UserControl1.xaml中的stkSomePanel(我们想要的)隐藏它,onload - 代码的大部分值都是Visible,所以我们可以看到它并尝试隐藏它 - 问题的一部分是知道放在哪里.Hidden和{ {1}}也是。按钮文字必须更改为"编辑"到"保存"。再次单击时,StackPanel的可见性需要切换回隐藏状态,文本返回"编辑"。

应该是一个简单的概念,但不清楚如何绑定什么到什么。我在父母上有一个按钮,我试图点击我打算隐藏的子按钮,但不确定我是否需要子按钮。我已经测试了这种变体,我可以单击子按钮,它会更新子按钮的按钮文本和子StackPanel的可见性一次,表明它运行.Visible,但是&#39 ; s只是一次性设置,而不是切换,如果我点击父按钮就没有任何事情发生,这真的需要工作。

到目前为止,MainWindow.xaml:

ClickHereExecuted()

MainWindow.xaml.cs:

<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>

UserControl1.xaml:

public partial class MainWindow : Window
{
    public static RoutedCommand ClickHereCommand {get; set;}

    public MainWindow()
    {
        InitializeComponent();
    }
}

UserControl1.xaml.cs:

<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>

2 个答案:

答案 0 :(得分:2)

我实际上发现我可以在没有绑定的情况下执行此操作,没有DependencyProperty,没有get-set,没有布尔转换器,没有StaticResources,没有Command。只是一个好的&#39;塑造Click EventHandler。我不知道如何在不进行绑定的情况下访问子进程内的对象(UserControl),因为我发现互联网上的所有东西都说使用这些东西并进行绑定。 这是 SO 不需要!!! 我浪费了很多时间,而且互联网上没有任何东西我发现说我可以做任何不同的事情。

基本上,要获得任何子控件,您只需要在UserControl上设置一个名称,我将其设置为UC1:

<v:GreatUserControl x:Name="UC1" />

我的MainWindow按钮现在看起来像这样:

<Button Content="Edit" Click="btnEdit_Click"/>

然后,在我的MainWindow的代码隐藏,onload中,我使用&#34; dot&#34;隐藏了孩子的StackPanel。语法off UC1

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        UC1.stkSomePanel.Visibility = Visibility.Hidden;
    }
}

此外,我在btnEdit_Click中有一个函数MainWindow.xaml.cs,我以相同的方式引用StackPanel:

private void btnEdit_Click(object sender, EventArgs e)
{
    Button btnEdit = (Button)sender;
    string btnText = btnEdit.Content.ToString();

    if (btnText == "Edit")
    {
        UC1.stkSomePanel.Visibility = Visibility.Visible;
        btnEdit.Content = "Save";
    }
    else
    {
        UC1.stkSomePanel.Visibility = Visibility.Hidden;
        btnEdit.Content = "Edit";
    }
}

这是我需要的所有代码。

答案 1 :(得分:0)

<Button 
    Content="{Binding ButtonContent, RelativeSource={RelativeSource AncestorType=UserControl}}" 
    Command="{Binding ClickHereCommand, RelativeSource={RelativeSource AncestorType=UserControl}}" 
    />
<StackPanel 
    x:Name="stkSomePanel" 
    Visibility="{Binding vis, RelativeSource={RelativeSource AncestorType=UserControl}, Converter={StaticResource ConvBoolToVis}}"
    >

ElementName=UserControl1可行,IIRC,如果用户控件XAML的根UserControl元素有x:Name="UserControl1"ElementName="Foo"表示&#34;范围内的元素其x:Name属性等于&#39; Foo&#39;&#34;。我最近还没有完成它,但我的回忆是,文件的根元素没有特殊情况(为什么会这样,对吧?)所以它应该有效。

但无论如何,所有这些绑定都在相同的上下文中解析,并且在每种情况下,您都绑定到UserControl子类的属性,因此它们都使用相同的源。

您可以将PresentationTraceSources.TraceLevel=High添加到任何Binding,并在VS的Output窗格中获取大量调试信息,以显示它为解决源代码所做的工作Binding,如果失败则失败。好东西,非常方便。

<Button 
    Content="{Binding ButtonContent, PresentationTraceSources.TraceLevel=High, RelativeSource={RelativeSource AncestorType=UserControl}}" 
    Command="{Binding ClickHereCommand, RelativeSource={RelativeSource AncestorType=UserControl}}" 
    />