繁体   English   中英

WPF:双向数据绑定不起作用

[英]WPF: two way data binding is not working

我发现了一些类似的问题,但这些并不是我所需要的。 我想将堆栈面板“IsEnabled”值绑定到我的项目的布尔值“!IsIterrupted”。 这是我的 XAML 文件:

<ListView ItemsSource="{Binding Path=Items}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <StackPanel IsEnabled="{Binding !IsInterrupted, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
                    <Button Command="{Binding Path=StopThreadCommand, Source={StaticResource viewModel}}" CommandParameter="{Binding Id}"/>
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

这是项目的样子:

public class ThreadDecorator : BaseThread , INotifyPropertyChanged
{
    ...
    public event PropertyChangedEventHandler PropertyChanged;

    private bool _is_interrupted;
    public bool IsInterrupted
    {
        get { return _is_interrupted; }
        set
        {
            _is_interrupted = value;
            OnPropertyChanged("IsInterrupted");
        }
    }

    protected virtual void OnPropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    ...
}

还有我的视图模型:

public class ThreadsViewModel : DependencyObject
{

    private ThreadsModel _model;
    public ThreadsModel Model
    {
        get { return _model; }
        set
        {
            _model = value;
        }
    }

    public ICollectionView Items
    {
        get { return (ICollectionView)GetValue(ItemsProperty); }
        set { SetValue(ItemsProperty, value); }
    }

    public static readonly DependencyProperty ItemsProperty =
        DependencyProperty.Register("Items", typeof(ICollectionView), typeof(ThreadsViewModel), new PropertyMetadata(null));

    public StopThreadCommand StopThreadCommand { get; set; }

    public ThreadsViewModel()
    {
        this.Model = new ThreadsModel();
        Items = CollectionViewSource.GetDefaultView(Model.Threads);
        this.StopThreadCommand = new StopThreadCommand(this);
    }

    public void InterruptThread(int id)
    {
        _model.InterruptThread(id);
    }
}

停止线程命令:

public class StopThreadCommand : ICommand
{
    public ThreadsViewModel ViewModel {get; set;}
    public StopThreadCommand(ThreadsViewModel viewModel)
    {
        this.ViewModel = viewModel;
    }

    public bool CanExecute(object parameter)
    {
        return true;
    }

    public void Execute(object parameter)
    {
        this.ViewModel.InterruptThread((int)parameter);
    }
}

当我单击“停止”按钮时,IsInterrupted 值从 false 变为 true,并且必须禁用堆栈面板,但 UI 不会更新。 请帮忙!

Binding的默认属性Path ,它是DataContext的属性/子属性的路径。 它不是任意的 C# 表达式。 所以你将Binding.Path设置为"!IsInterrupted" !IsInterrupted不会计算为IsInterrupted的布尔逆; 它不会评估任何东西。 它会在调试输出流中为您提供:

System.Windows.Data 错误:40:BindingExpression 路径错误:在“对象”“ThreadDecorator”上找不到“!IsInterrupted”属性等等

<StackPanel 
    IsEnabled="{Binding !IsInterrupted, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">

一种方法是编写一个布尔值-逆值转换器(从该链接另一端的 Chris Nicol 的回答中逐字窃取):

[ValueConversion(typeof(bool), typeof(bool))]
public class InverseBooleanConverter: IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter,
        System.Globalization.CultureInfo culture)
    {
        if (targetType != typeof(bool))
            throw new InvalidOperationException("The target must be a boolean");

        return !(bool)value;
    }

    public object ConvertBack(object value, Type targetType, object parameter,
        System.Globalization.CultureInfo culture)
    {
        throw new NotSupportedException();
    }

    #endregion
}

用法:

<UserControl.Resources>
    <local:InverseBooleanConverter x:Key="InverseBooleanConverter" />
</UserControl.Resources>

<!-- stuff etc. -->

IsEnabled="{Binding Path=IsReadOnly, Converter={StaticResource InverseBooleanConverter}}"

您还可以编写带有DataTrigger的 Style,如果IsInterrupted为 true,则将IsEnabled设置为False

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM