如何在点击内容后隐藏展开? XAML
How to hidden Expand after clicking on the content? XAML
需要一些帮助,有谁知道如何做到当您单击“展开”的内容时 - 它会隐藏吗?
<Expander Header="First Expand">
<ListBox>
<ListBox.Items>
<CheckBox Content="First"/>
<CheckBox Content="Second"/>
<CheckBox Content="Third"/>
</ListBox.Items>
</ListBox>
</Expander>
是否可以使用绑定?
这是我使用绑定编写的示例。如果您 "check" 第三个复选框,扩展器将关闭。请注意,如果您取消选中扩展器,它不会关闭(这只是我的设计,您的需求可能会有所不同)。
<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))); } }
}
我使用 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;
}
我认为这不是一个好主意
需要一些帮助,有谁知道如何做到当您单击“展开”的内容时 - 它会隐藏吗?
<Expander Header="First Expand">
<ListBox>
<ListBox.Items>
<CheckBox Content="First"/>
<CheckBox Content="Second"/>
<CheckBox Content="Third"/>
</ListBox.Items>
</ListBox>
</Expander>
是否可以使用绑定?
这是我使用绑定编写的示例。如果您 "check" 第三个复选框,扩展器将关闭。请注意,如果您取消选中扩展器,它不会关闭(这只是我的设计,您的需求可能会有所不同)。
<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))); } }
}
我使用 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;
}
我认为这不是一个好主意