无法使用样式禁用WPF扩展器

时间:2016-08-31 07:30:12

标签: c# wpf expander isenabled

我为我的Expander控件创建了一个样式,如果它的值是 false ,它应该禁用扩展器,但它根本不起作用。无论IsCheckedIn是真还是假,我的扩展器都会一直启用。

<Style x:Key="CollapseIsCheckedInExpander" TargetType="{x:Type Expander}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding IsCheckedIn}" Value="False">
            <Setter Property="IsEnabled" Value="False"/>
        </DataTrigger>
    </Style.Triggers>
</Style>  

这里是我在数据网格中设置扩展器样式的地方:

<Expander Expanded="Expander_Expanded" Collapsed="Expander_Expanded" Style="{DynamicResource CollapseIsCheckedInExpander}"/>

我有一个名为“IsCheckedIn”的属性代码隐藏,我将值设置为 true false ,具体取决于我正在办理的卡车是否已经签到或不签。

有什么我想念的吗?

编辑:

这是我的班级:

public class TruckItems : INotifyPropertyChanged
{        
    bool _isCheckedIn;
    public bool IsCheckedIn
    {
        get { return _isCheckedIn; }
        set
        {
            if (_isCheckedIn != value)
                _isCheckedIn = value;
            OnPropertyChanged("IsCheckedIn");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }        
}

这是我在定义IsCheckedIn:

的剪辑
truckItem.TruckCurrentPhase = item.CurrentPhase; //Current value is "Not Started", so the IsCheckedIn value should be false in the code below
truckItem.IsCheckedIn = truckItem.TruckCurrentPhase == "Checked In" ? true : false;

我发现我的问题是使用RowHeaderTemplate。出于某种原因,它没有拿起我的绑定,但DataGridTemplateColumn确实......

这不起作用:

<DataGrid.RowHeaderTemplate>
    <DataTemplate>
        <Expander Expanded="Expander_Expanded" Collapsed="Expander_Expanded" Style="{StaticResource CollapseIsCheckedInExpander}"/>
    </DataTemplate>
</DataGrid.RowHeaderTemplate>

这确实有效:

<DataGridTemplateColumn>
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Expander Expanded="Expander_Expanded" Collapsed="Expander_Expanded" Style="{StaticResource CollapseIsCheckedInExpander}"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

1 个答案:

答案 0 :(得分:1)

我建立了一个简单的例子(使用Caliburn Micro进行绑定和通知......)。它对我来说很好。

通过这个你可以简单地测试Binding。在应用程序运行时设置两个断点。如果更改复选框,Breakpoint1应该触发,然后Breakpoint2应该触发(我想两次,获取CheckBox的实际值和Expander IsEnabled的实际值)。如果断点没有触发,你必须检查你的DataContext(在原始代码中,DataContext需要是一个truckItem而不是你的ViewModel ......你有没有检查过这个?)。

XAML

 <CheckBox IsChecked="{Binding ExpanderEnable, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">Enalble Expander</CheckBox>
 <Expander IsEnabled="{Binding ExpanderEnable, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}">
     <TextBlock>TestText</TextBlock>
 </Expander>

CS

 private bool _expanderEnable;

 public bool ExpanderEnable {
     get 
     { 
         return _expanderEnable; //Breakpoint2
     }
     set {
         if (value == _expanderEnable) return; //BreakPoint1
         _expanderEnable = value;
         OnPropertyChanged();
     }
 }

public event PropertyChangedEventHandler PropertyChanged;

[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
相关问题