如何从另一个扩展器的IsExpanded属性触发WPF扩展器IsExpanded属性

时间:2010-10-06 02:51:48

标签: wpf xaml triggers

我有两个并排的扩张器。我希望一次只扩展一个。因此,如果一个扩展,并且用户扩展另一个,我希望第一个崩溃。用户可以折叠,折叠两者都是起始状态。

从代码中可以看出,我已将“Header”属性作为测试包含在内,并且按预期工作,但IsExpanded属性不起作用。

                                                                                                                                        

<Expander x:Name="emailExpander">
  <Expander.Style>
    <Style TargetType="Expander">
      <Setter Property="IsExpanded" Value="False"/>
      <Setter Property="Header" Value="Email"/>
      <Style.Triggers>
        <DataTrigger Binding="{Binding IsExpanded,ElementName=customerExpander}" Value="True">
          <Setter Property="IsExpanded" Value="False"/>
          <Setter Property="Header" Value="other expanded"/>
        </DataTrigger>
      </Style.Triggers>
    </Style>
  </Expander.Style>      
</Expander>

3 个答案:

答案 0 :(得分:1)

这可以通过添加一点逻辑来绑定到视图对象来处理。

在WPF中,将IsExpanded属性绑定到视图的EmailExpanded和CustomerExpanded属性。

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="1*"/>
        <ColumnDefinition Width="1*"/>
    </Grid.ColumnDefinitions>
    <Expander Grid.Column="0" Header="Email" IsExpanded="{Binding EmailExpanded}">
        <TextBlock Text="Email Data"/>
    </Expander>
    <Expander Grid.Column="1" Header="Customer" IsExpanded="{Binding CustomerExpanded}">
        <TextBlock Text="Customer Data"/>
    </Expander>
</Grid>

在主窗口中指定视图。

public MainWindow()
{
    InitializeComponent();

    DataContext = new View();
}

然后使您的视图类如下所示。

class View : INotifyPropertyChanged
{
    private bool _CustomerExpanded;
    public bool CustomerExpanded
    {
        get
        {
            return _CustomerExpanded;
        }
        set
        {
            if (_CustomerExpanded != value)
            {
                // Add logic to close Email Expander
                if (value)
                {
                    EmailExpanded = false;
                }

                _CustomerExpanded = value;
                OnPropertyChanged("CustomerExpanded");
            }
        }
    }

    private bool _EmailExpanded;
    public bool EmailExpanded
    {
        get
        {
            return _EmailExpanded;
        }
        set
        {
            if (_EmailExpanded != value)
            {
                // Add logic to close Customer Expander
                if (value)
                {
                    CustomerExpanded = false;
                }

                _EmailExpanded = value;
                OnPropertyChanged("EmailExpanded");
            }
        }
    }

    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    public event PropertyChangedEventHandler PropertyChanged = delegate { };
}

注意添加到setter。折叠扩展器对其他扩展器没有影响,但扩展扩展器将导致另一个扩展器崩溃。没有堆栈溢出:)

答案 1 :(得分:0)

我在这篇文章中找到了答案: WPF Expanders Triggers

在上面的答案中使用BoolInverterConverter,这里是您的案例的代码片段

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication1"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <local:BoolInverterConverter x:Key="bic"/>
</Window.Resources>
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <Expander x:Name="emailExpander" IsExpanded="{Binding ElementName=customerExpander, Converter={StaticResource bic}, Path=IsExpanded}">
        <Expander.Style>
            <Style TargetType="Expander">
                <Setter Property="Header" Value="Email"/>
            </Style>
        </Expander.Style>
        <StackPanel Margin="10,4,0,0">
            <CheckBox Margin="4" Content="Email 1" />
            <CheckBox Margin="4" Content="Email 2" />
            <CheckBox Margin="4" Content="Email 3" />
        </StackPanel>
    </Expander>
    <Expander x:Name="customerExpander" Grid.Column="1"> 
        <Expander.Style>
            <Style TargetType="Expander">
                <Setter Property="Header" Value="Customer"/>
            </Style>
        </Expander.Style>
        <StackPanel Margin="10,4,0,0">
            <CheckBox Margin="4" Content="Customer 1" />
            <CheckBox Margin="4" Content="Customer 2" />
            <CheckBox Margin="4" Content="Customer 3" />
        </StackPanel>
    </Expander>
</Grid>

答案 2 :(得分:0)

你最好做的是使用WPF Toolkit V2中发布的手风琴控件。非常方便,没有“Stack Overflow”例外。 =)