我有多个首先折叠的堆叠面板,当点击一个按钮时,与按钮点击相关的堆栈面板变得可见。现在,这就是我的工作原理。
private void Button1_Click(object sender, RoutedEventArgs e)
{
dgrid.ItemsSource = FillDataGrid("ShipWorksConnection", "GetPicklistItems", "PickList");
SP1.Visibility = Visibility.Visible;
SP2.Visibility = Visibility.Collapsed;
SP3.Visibility = Visibility.Collapsed;
SP4.Visibility = Visibility.Collapsed;
}
private void Button2_Click(object sender, RoutedEventArgs e)
{
dgrid.ItemsSource = FillDataGrid("SupplyConnection", "GetAllSupplies", "Supplies");
SP1.Visibility = Visibility.Collapsed;
SP2.Visibility = Visibility.Visible;
SP3.Visibility = Visibility.Collapsed;
SP4.Visibility = Visibility.Collapsed;
}
等等,用于多个按钮点击事件。这是处理这个问题的方法,因为我将添加更多按钮点击事件,我还可以做些其他事情来使代码更具可重用性。
答案 0 :(得分:0)
如果隐藏/显示元素中存在某些组合逻辑,则可以使用绑定来切换可见性。
对于此示例,您必须创建一个viewmodel并在其中创建一个属性TheConditionProperty
。然后将视图模型绑定到控件。在您的场景中,您甚至可以绑定到一个项目数组,这将比一次又一次地添加属性更容易。
<StackPanel>
<StackPanel.Style>
<Style TargetType="StackPanel">
<Style.Triggers>
<DataTrigger Binding="{Binding TheConditionProperty}" Value="False">
<Setter Property="Visibility" Value="Collapsed" />
</DataTrigger>
</Style.Triggers>
</Style>
</StackPanel.Style>
</StackPanel>
答案 1 :(得分:0)
WPF开发人员通常不在程序代码中设置控件的Visibility
属性。相反,为您要显示或隐藏的每个控件声明bool
属性并使用BooleanToVisibilityConverter
将bool
值转换为Visibility
更为常见} value:
<SomeControl Visibility={Binding IsSomeControlVisible,
Converter={StaticResource BooleanToVisibilityConverter}} ... />
然后要显示此控件,您只需将IsSomeControlVisible
属性设置为true
并隐藏它,即可将其设置为false
。
答案 2 :(得分:0)
add Simple button click(Button_Click) to all the button (e.g button1 , button2)... In below code assume that u had button name property as button1 and button2.
private void Button_Click(object sender, RoutedEventArgs e)
{
Button b = sender as Button;
List<StackPanel> CollapsedStackPanel = new List<StackPanel>();
if (b.Name == Button1.Name)
{
dgrid.ItemsSource = FillDataGrid("ShipWorksConnection", "GetPicklistItems", "PickList");
CollapsedStackPanel.Add(SP2); //add only stackpanel that u want set Collapsed
CollapsedStackPanel.Add(SP3); //add only stackpanel that u want set Collapsed
CollapsedStackPanel.Add(SP); //add only stackpanel that u want set Collapsed
}
else if (b.Name == Button2.Name)
{
dgrid.ItemsSource = FillDataGrid("ShipWorksConnection", "GetPicklistItems", "PickList");
CollapsedStackPanel.Add(SP1);
CollapsedStackPanel.Add(SP3); //add only stackpanel that u want set Collapsed
CollapsedStackPanel.Add(SP4); //add only stackpanel that u want set Collapsed
}
//can add here more else if other buttons as well
SetStackPanelVisibility(CollapsedStackPanel);
}
private void SetStackPanelVisibility(List<StackPanel> CollapsedStackPanel)
{
//get all the stackpanel in current window
//if its in visible list then set its visiblity to visible
// else set its visibility to collapsed
foreach (StackPanel item in FindVisualChildren<StackPanel>(this))
{
if (CollapsedStackPanel.Contains(item))
{
item.Visibility = System.Windows.Visibility.Collpsed;
}
}
}
public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
{
//preferably put this code in some common place so u can use whenever u want.
if (depObj != null)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
if (child != null && child is T)
{
yield return (T)child;
}
foreach (T childOfChild in FindVisualChildren<T>(child))
{
yield return childOfChild;
}
}
}
}
答案 3 :(得分:0)
好吧,如果没有组合逻辑,那么你至少可以&#34;快捷方式&#34;它,只是一个想法,你可以解决强烈打字。
ViewHelper.SetBulkVisibility(Visibility.Collapsed, new List<dynamic> {SP1, SP2, SP3});
public static class ViewHelper
{
public static void SetBulkVisibility(Visibility value, IEnumerable<dynamic> objects)
{
foreach (var o in objects)
{
o.Visibility = value;
}
}
}
或
可能用类似的方法来反转列表中对象的当前可见性。