需要帮助,有人知道如何制作它,以便当您单击Expand的内容时-它是否隐藏?
<Expander Header="First Expand">
<ListBox>
<ListBox.Items>
<CheckBox Content="First"/>
<CheckBox Content="Second"/>
<CheckBox Content="Third"/>
</ListBox.Items>
</ListBox>
</Expander>
是否可以使用绑定?
答案 0 :(得分:1)
这是我使用绑定编写的示例。如果“选中”第三个复选框,则扩展器关闭。请注意,如果您取消选中扩展器,它不会关闭(这仅是我的设计,您的需求可能会有所不同)。
<Expander x:Name="firstExpand" Header="First Expand" IsExpanded="{Binding IsExpanded}">
<ListBox>
<ListBox.Items>
<CheckBox x:Name="firstCheckbox" Content="First" IsChecked="{Binding FirstChecked}"/>
<CheckBox x:Name="secondCheckbox" Content="Second" IsChecked="{Binding SecondChecked}"/>
<CheckBox x:Name="thirdCheckbox" Content="Third" IsChecked="{Binding ThirdChecked}"/>
</ListBox.Items>
</ListBox>
</Expander>
public partial class MainWindow : Window, INotifyPropertyChanged
{
private bool firstChecked;
private bool secondChecked;
private bool thirdChecked;
private bool isExpanded;
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
this.PropertyChanged += MainWindow_PropertyChanged;
this.IsExpanded = true;
}
private void MainWindow_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
// If the third item was checked...close the expansion
if (e.PropertyName == nameof(ThirdChecked) && ThirdChecked)
{
this.IsExpanded = false;
}
}
public event PropertyChangedEventHandler PropertyChanged;
public bool FirstChecked { get => this.firstChecked; set { this.firstChecked = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(FirstChecked))); } }
public bool SecondChecked { get => this.secondChecked; set { this.secondChecked = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(SecondChecked))); } }
public bool ThirdChecked { get => this.thirdChecked; set { this.thirdChecked = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(ThirdChecked))); } }
public bool IsExpanded { get => this.isExpanded; set { this.isExpanded = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsExpanded))); } }
}
答案 1 :(得分:0)
我使用ToggleButton,并重写模板(例如Expander,但Name = Header)
<ToggleButton Style="{DynamicResource NewToggle}"
Name="First Toggle" Click="Toggle_click">
<ListBox>
<ListBox.Items>
<CheckBox Content="First"/>
<CheckBox Content="Second"/>
<CheckBox Content="Third"/>
</ListBox.Items>
</ListBox>
</ToggleButton>
然后在后面使用代码:
private void Toggle_click(object sender, RoutedEventArgs e)
{
if (e.OriginalSource.GetType().Name == "CheckBox")
{
var myCheck= (CheckBox)e.OriginalSource;
var MyList = (ListBox)myCheck.Parent;
var myToogle = (ToggleButton)MyList.Parent;
myToogle.IsChecked = false;
}
return;
}
我认为这不是一个好主意