2016-09-20 18 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"; 
    } 
} 

這是所有的代碼我需要。

相關問題