c# - 无法使用样式禁用 WPF 扩展器

标签 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”的属性代码隐藏,我将值设置为 truefalse,具体取决于我正在登记的卡车是否已经 checkin 与否。

有什么我想念的吗?

编辑:

这是我的类(class):

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。出于某种原因,它没有获取我的绑定(bind),但 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>

最佳答案

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

有了这个你就可以简单的测试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));
}

关于c# - 无法使用样式禁用 WPF 扩展器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39243345/

相关文章:

c# - 我如何才能发现最终用户的用户系统性能设置?

c# - 如何在 WinForms 中更新 DataGridView

c# - WPF 从 RefreshEvent 更新 UI

wpf - 获取非 UI 对象来响应 WPF 命令绑定(bind)

wpf - 当 slider 位于 StackPanel 内时,如何扩展 slider 以填充可用空间

wpf - 如何使用 IsEnabled 来禁用 Expander 的部分而不是全部?

c# - 以编程方式添加多个扩展器事件将不起作用

wpf - 当 ListView 中的扩展器折叠时,无法使 WPF ListView 折叠

c# - 在没有类约束的情况下将一个泛型转换为另一个泛型

c# - 有没有办法验证字典的值