2016-09-20 68 views
-1

所以我在这里看到过类似的问题,但一直没能指出这一点。我们有一个页面,其中包含一个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 
    } 
} 
+0

如果它得到了你的正确,你想要在窗口中的按钮和在UC的StackPanel。当点击窗口中的按钮时,该StackPanel应该隐藏? – AnjumSKhan

+0

@AnjumSKhan是的。 – vapcguy

+0

@AnjumSKhan更准确地说,按钮会在UC中显示隐藏的StackPanel,并在再次单击时将其隐藏。文本将从“编辑”(默认)更改为“保存”,并显示StackPanel。当再次点击时,“保存”变为“编辑”并且StackPanel再次被隐藏。 – vapcguy

回答

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"指“其具有x:Name属性等于‘富’在范围元素”。最近我还没有做,但我的回忆是,没有特殊的情况下文件的根元素(为什么会有,对吧?),所以它应该工作。

但无论如何,所有这些绑定都在相同的上下文中解析,并且在每种情况下,您都绑定到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}}" 
    /> 
+0

谢谢 - 确实是好东西,但是厌倦了所有绑定的东西。对我来说,真正不直观的是,为了操作正在被设置的东西,即如果按钮的命令绑定到StackPanel可见性,那么它如何改变文本?如果按钮的命令绑定到文本,它如何改变可视性?并再次切换回去?然后设置SP可见性 - 是否使用按钮的可见性?在“DependencyProperty”中设置它或者在窗口/控件上加载它很容易,而不是在不同的情况下运行。 – vapcguy

+0

@vapcguy命令的execute方法会设置'vis'和'ButtonContent'属性。这种做法“正确的方式”有一个陡峭的学习曲线,但“错误的方式”是非常有限的,不适合非平凡的发展。即使在我开始考虑它的时候,我仍然用一种紫色的激情憎恶WPF,因为我第一年或更长时间与它合作。这就像从程序编程转向面向对象:你正在学习一种思考事物的新方式,新方法的价值在你开始立足之前并不明显。 –

+0

@vapcguy我的意思是,你的Click事件显然会做你想做的事情。这对于您当前的需求已经足够了。如果有人与我一起工作,那么我就不会投诉。然而,根据我的经验,这种做事方式不能很好地适应更复杂的代码。 –

1

我居然发现我能做到这一点没有约束力,没有DependencyProperty,没有一个get-set,没有布尔转换器,没有StaticResources,没有Command。只是一个很好的ol'老式Click EventHandler。我只是不确定如何在没有绑定的情况下访问子对象(UserControl)中的对象,因为我发现互联网上的所有东西都说要使用这些东西并进行绑定。 这是SO不需要!我浪费了SO所有这些都花费了很多时间,而且互联网上没有任何东西,我发现说我可以做任何不同的事情。

基本上得到任何子控件,你就必须有一个名字上的用户控件,这是我设置为UC1设置:

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

我的主窗口的按钮现在看起来是这样的:

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

然后,在我的主窗口的代码隐藏,onload事件,我做的StackPanel上隐藏着一个“点”语法孩子断UC1

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

此外,我在MainWindow.xaml.cs函数btnEdit_Click其中I引用的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"; 
    } 
} 

这是所有的代码我需要。

相关问题