2013-03-08 67 views
0

我的應用程序中有一個UserInput窗體,其中包含豐富的文本框和數據網格。有些客戶端不會每次都使用它們...所以我希望一些用戶點擊按鈕或複選框時動態顯示。讓控制在WPF窗體C#VS 2010中動態顯示

我試過尋找在線幫助,但沒有運氣。

預先感謝您的任何建議

+0

如果您的查詢是關於顯示/隱藏控件,您是否嘗試在面板上切換「可見性」屬性?或者您是否想通過點擊按鈕動態地將控件添加到Form中? – 2013-03-08 07:42:16

+0

多數民衆贊成它!我只是想能夠顯示和隱藏一些控制 – 2013-03-08 07:57:20

+0

@Siva Thanx Siva,這是可行的,但它不會真正解決空間問題。我想讓它在點擊複選框的下方彈出,其餘複選框向下移動 – 2013-03-08 08:02:32

回答

0

既然你是剛開始使用WPF,我想向您展示一些樣品可以在其他的東西扔光像觸發器等,這可能會幫助你..

<Window x:Class="WpfApplication1.Window1" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="Window1" Height="300" Width="300" 
     > 
    <StackPanel> 

     <ToggleButton x:Name="tgbtnTextBoxVisibility" Click="tgbtnTextBoxVisibility_Click" > 
      <!-- Define the Style Trigger, to toggle the Text on the Toggle Button. If the ToggleButton is unchecked then display 'Hide TextBoxes'--> 
      <!---Do not set the 'Content' property directly on the control, which overrides the 'Content' defined in Styles/Triggers, because of property precedence--> 
      <ToggleButton.Style> 
       <Style TargetType="ToggleButton"> 
        <Setter Property="Content" Value="Show TextBoxes"/> 
        <Style.Triggers> 
         <Trigger Property="IsChecked" Value="True" > 
          <Setter Property="Content" Value="Hide TextBoxes"/> 
         </Trigger> 
        </Style.Triggers> 
       </Style> 
      </ToggleButton.Style> 
     </ToggleButton> 

     <!-- When the StackPanel below is shown, you can observe the 'Show TextBlocks' button automatically slides down--> 
     <StackPanel x:Name="spTextBoxes" Visibility="Collapsed"> 
      <TextBox x:Name="txtName" Width="100" Height="30"/> 
      <TextBox x:Name="txtCompany" Width="100" Height="30"/> 
     </StackPanel> 

     <ToggleButton x:Name="tgbtnTextBlockVisibility" Click="tgbtnTextBlockVisibility_Click"> 
      <ToggleButton.Style> 
       <Style TargetType="ToggleButton"> 
        <Setter Property="Content" Value="Show TextBlocks"/> 
        <Style.Triggers> 
         <Trigger Property="IsChecked" Value="True" > 
          <Setter Property="Content" Value="Hide TextBlocks"/> 
         </Trigger> 
        </Style.Triggers> 
       </Style> 
      </ToggleButton.Style> 
     </ToggleButton> 
     <StackPanel x:Name="spTextBlocks" Visibility="Collapsed"> 
      <!--'Text' property of TextBlock is directly bound to the corresponding TextBox's 'Text' properrty, which changes as you type in the text into source TextBox--> 
      <TextBlock x:Name="tbkName" Text="{Binding ElementName=txtName,Path=Text}" Width="100" Height="30"/> 
      <TextBlock x:Name="tbkCompany" Text="{Binding ElementName=txtCompany,Path=Text}" Width="100" Height="30"/> 
     </StackPanel> 

    </StackPanel> 
</Window> 

,並在代碼隱藏我正在設置相應的堆疊面板的可見性。 注意:使用ValueConverter更改Visiblity可能更合適。

/// <summary> 
    /// Interaction logic for Window1.xaml 
    /// </summary> 
    public partial class Window1 : Window 
    { 
     public Window1() 
     { 
      InitializeComponent(); 
     } 

     private void tgbtnTextBoxVisibility_Click(object sender, RoutedEventArgs e) 
     { 
      ToggleButton tgbtnTextBoxes=sender as ToggleButton; 

      if (tgbtnTextBoxes.IsChecked.HasValue && tgbtnTextBoxes.IsChecked.Value) 
       spTextBoxes.Visibility = Visibility.Visible; 
      else 
       spTextBoxes.Visibility = Visibility.Collapsed; //Note: Visibility.Collapsed will resize the layout where as Visibility.Hidden will not. 
     } 

     private void tgbtnTextBlockVisibility_Click(object sender, RoutedEventArgs e) 
     { 
      ToggleButton tgbtnTextBlocks = sender as ToggleButton; 

      if (tgbtnTextBlocks.IsChecked.HasValue && tgbtnTextBlocks.IsChecked.Value) 
       spTextBlocks.Visibility = Visibility.Visible; 
      else 
       spTextBlocks.Visibility = Visibility.Collapsed; //Note: Visibility.Collapsed will resize the layout where as Visibility.Hidden will not. 
     } 
    } 
1

1)首先最基本的問題 - 你需要文本框和數據網格豐盈?這聽起來像是有可能造成混淆的用戶界面。我總是試着首先簡化用戶界面。

2)控件是否必須處於固定佈局?這與將控件的可見性打開和關閉一樣簡單嗎? 3)如果控件佈局必須是動態的,則可以動態地將控件「.Add(new Button(...))」添加到列表/網格中。我不確定我會推薦這種方法,不僅僅是一些簡單的改變。

4)是否有對所有客戶都通用的控件?

5)如果有通用控件則可以考慮使用一個用戶控件來組合這些並使用ContentPresenter或其它控​​制(這樣做的具體方式可能取決於您使用的是MVVM架構或者動態地將它們添加到窗體不)。您可以將contentpresenter綁定到屬​​於UserControl的屬性。

6)最糟糕的情況是佈局必須對每個客戶端非常靈活,以至於您只需爲每個客戶端創建一個新的UserControl。您綁定的基礎數據對象可以保持不變,但只是通過每個UserControl暴露不同的方面。

你對WPF有多熟悉?要真正有效地完成這種事情,您需要研究數據綁定,視圖,內容演示者,列表/網格,UserControls。

+0

1)我確實需要它,它是我爲一家制造公司編寫的程序,這是一個簡單的用戶界面程序,允許用戶輸入其客戶要求的尺寸,然後應用程序生成製造規格,切割尺寸等女巫也將被鏈接到數據庫並與機器集成。 2)是的,我希望它在彈出的複選框被點擊下,其餘的複選框向下移動。4)是的,該公司目前使用Excel表格程序,我只給他們他們想要的東西。 – 2013-03-08 07:52:22

+0

我是一名電氣工程師,所以我對c#的使用經驗並不多。但我很容易適應它,因爲我做了C和Java幾年..我大部分都是機器編碼VHDL,但我還需要編寫一個用戶界面應用程序 – 2013-03-08 07:55:25

+0

我會試着去解答這個問題多一點根據你的答案,但我不能做到這一點4-5小時。 – 2013-03-08 10:04:20