【问题标题】:How to hidden Expand after clicking on the content? XAML点击内容后如何隐藏展开? XAML
【发布时间】:2020-09-20 02:55:39
【问题描述】:

需要一些帮助,有谁知道如何做到这一点,以便当您单击展开的内容时 - 它会隐藏吗?

        <Expander Header="First Expand">
            <ListBox>
                <ListBox.Items>
                    <CheckBox Content="First"/>
                    <CheckBox Content="Second"/>                                    
                    <CheckBox Content="Third"/>
                </ListBox.Items>
            </ListBox>
        </Expander>

是否可以使用绑定?

【问题讨论】:

  • 或触发器,或其他明确的 xaml

标签: wpf xaml expand


【解决方案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))); } }
    }

【讨论】:

  • 我的意思是属性绑定)就像清晰的 xaml,后面没有代码。谢谢你的回答,但是.. 它看起来并不“优雅”。此外,您可以使用“Click”(不是“IsChecked”)
  • 哦,“这只是我的设计”好吧,你
【解决方案2】:

我使用 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;
}

我认为这不是一个好主意

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-24
    • 2013-09-04
    • 1970-01-01
    • 1970-01-01
    • 2017-05-01
    • 1970-01-01
    相关资源
    最近更新 更多