我有一個帶兩個單選按鈕的堆疊面板。一個是選項A和另一個選項B.一次只能選擇一個。當我檢查選項B時,我想在選項B radiobuttion下面和堆棧面板中顯示幾個文本框。當我再次選擇選項A時,文本框不應該可見。我怎樣才能通過使用XAML來實現這一點?WPF Stack Panel show hide children
回答
只有在XAML甚至沒有轉換器。定義控件模板在你的資源,如
<Window.Resources>
<ControlTemplate x:Key="RadioButtonContent">
<Grid>
<StackPanel Margin="0,0,0,202">
<RadioButton x:Name="OptionA" Content="OptionA" />
<RadioButton x:Name="OptionB" Content="OptionB" />
</StackPanel>
<TextBox x:Name="MyTextBox"
Visibility="Visible"
Text="My Textbox" Margin="0,65,165,167"/>
</Grid>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding ElementName=OptionB, Path=IsChecked}" Value="False">
<Setter TargetName="MyTextBox" Property="Visibility" Value="Hidden"/>
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Window.Resources>
,並使用它像
<ContentControl Template="{StaticResource RadioButtonContent}"/>
希望它有助於..
只要有一個事件,當您選擇選項A,無論是TextBoxes
獲取設置爲:
Textbox.Visibility = Collapsed or Hidden
取決於你想如何顯示它們。
這不是XAML –
是的。發佈後我才意識到這一點。 – JosephGarrone
您可以綁定到RadioButton
IsChecked
屬性,並使用內置BooleanToVisibilityConverter
<Window x:Class="WpfApplication4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication4"
Title="MainWindow" Height="300" Width="400" Name="UI" >
<Window.Resources>
<BooleanToVisibilityConverter x:Key="BoolToVisibilityConverter" />
</Window.Resources>
<Grid>
<StackPanel Margin="0,0,0,202">
<RadioButton x:Name="OptionA" Content="OptionA" />
<RadioButton x:Name="OptionB" Content="OptionB" />
</StackPanel>
<TextBox Visibility="{Binding ElementName=OptionB, Path=IsChecked, Converter={StaticResource BoolToVisibilityConverter}}" Text="My Textbox" Margin="0,65,165,167"/>
</Grid>
</Window>
這將顯示TextBox
只有當「OptionB」被選中。
試試這個代碼希望能解決你的問題。
將文本框放入堆棧面板並將堆棧面板的可見性屬性設置爲隱藏。 並在OptionButton事件上編寫代碼以設置stackPanel的Visibility屬性。
<Grid.RowDefinitions>
<RowDefinition Height="33*"></RowDefinition>
<RowDefinition Height="33*"></RowDefinition>
<RowDefinition Height="33*"></RowDefinition>
</Grid.RowDefinitions>
<StackPanel Margin="20" Orientation="Vertical" VerticalAlignment="Center" HorizontalAlignment="Left" Grid.Column="0" Grid.Row="0">
<RadioButton Name="rdA" Content="Option A" GroupName="txtBoxGroup" Checked="rdA_Checked"></RadioButton>
<RadioButton Name="rdB" Content="Option B" GroupName="txtBoxGroup" Checked="rdB_Checked"></RadioButton>
</StackPanel>
<StackPanel Margin="20" Name="TxtBxStackPanel" Height="auto" Orientation="Vertical" Grid.Column="0" Grid.Row="1" Visibility="Hidden">
<Label Content="My TextBox" VerticalAlignment="Center" HorizontalAlignment="Left"></Label>
<TextBox Name="txtValue" Height="20" Width="200" HorizontalAlignment="Left"></TextBox>
</StackPanel>
</Grid>
在代碼behid -
private void rdB_Checked(object sender, RoutedEventArgs e)
{
TxtBxStackPanel.Visibility = Visibility.Visible;
}
private void rdA_Checked(object sender, RoutedEventArgs e)
{
TxtBxStackPanel.Visibility = Visibility.Hidden;
}
享受!!!!!!
感謝
CK尼廷(丁丁)
有兩個問題。首先,可見性可能應該更改爲摺疊,而不是隱藏。其次,這不僅僅是XAML,正如問題中特別要求的那樣。 –
- 1. show hide image inside div
- 2. hide/show toggle div
- 3. java show hide frame
- 4. onmousedown hide/show div
- 5. jquery hide show
- 6. javascript show hide div
- 7. IE6 show hide divs
- 8. Hide/Show DataTemplate
- 9. hide show menu getElementsByClassName
- 10. jquery datatable show hide
- 11. javascript hide show
- 12. jquery show hide division
- 13. jquery show hide each div onlick
- 14. Angularjs ng-repeat hide/show
- 15. jquery onclick hide/show div
- 16. AngularJS ng-hide/ng-show
- 17. Hide&Show右側欄
- 18. AngularJS ng-show,ng-hide
- 19. Javascript:Ajax和Show/Hide Div
- 20. jQuery .show/.hide使用
- 21. jQuery $(this).closest show&hide
- 22. jquery dropdown show hide tr
- 23. Show and Hide Div onMouseOver
- 24. jQuery Modal + .show()和.hide()
- 25. jQuery Cookie hide/show div
- 26. Jquery Show/Hide in safari
- 27. Angular hide show not working
- 28. jquery hide \ show功能
- 29. jQuery .detach()vs hide/show
- 30. show panel link_to in active admin
可能重複:http://stackoverflow.com/questions/1534208/binding-to -a-wpf-togglebuttons-ischecked-state – WildCrustacean