【问题标题】:WPF: bind property from MainWindow.xaml cause an errorWPF:来自 MainWindow.xaml 的绑定属性导致错误
【发布时间】:2018-04-13 10:58:35
【问题描述】:

所以我有这个view model

public class WiresharkFiles : INotifyPropertyChanged
{
    public ObservableCollection<WiresharkFile> List { get; set; }
    public event PropertyChangedEventHandler PropertyChanged;
    private bool _inUse;
    private int _packets;
    private bool _hasItems;

    public WiresharkFiles()
    {
        List = new ObservableCollection<WiresharkFile>();
        HasItems = false;
        List.CollectionChanged += List_CollectionChanged;
    }

    private void List_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        HasItems = List.Count > 0;
    }

    public bool InUse
    {
        get { return _inUse; }
        set
        {
            _inUse = value;
            NotifyPropertyChanged("InUse");
        }
    }

    public int Packets
    {
        get { return _packets; }
        set
        {
            _packets = value;
            NotifyPropertyChanged("Packets");
        }
    }

    public bool HasItems
    {
        get { return _hasItems; }
        set
        {
            _hasItems = value;
            NotifyPropertyChanged("HasItems");
        }
    }

    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

MainWindow.xaml

private WiresharkFiles caps;

public MainWindow()
{
    InitializeComponent(); 
    caps = new WiresharkFiles();
}

Window.Resources

<Window.Resources>
   <Convertors:CollectionHasItemsConverter x:Key="CollectionHasItemsConverter"/>
</Window.Resources>

转换器

public class CollectionHasItemsConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return (bool)value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

我的收藏项目(是否为空)的基础我想启用/禁用我的Button

<Button Name="btnDeleteAll"
        Click="btnDeleteAll_Click"
        IsEnabled="{Binding Path=(caps.HasItems),Converter={StaticResource CollectionHasItemsConverter}}">

我得到了这个error

XamlParseException:类型引用找不到名为的类型 '{http://schemas.microsoft.com/winfx/2006/xaml/presentation}大写字母'。

【问题讨论】:

  • 除了大写是私有变量和未设置数据上下文之外,绑定中的圆括号也是问题。

标签: wpf binding


【解决方案1】:

我看不到您将DataContextcaps 属性相关联的位置。

确保您有一个公共属性,因为 WPF 引擎不是在您的类中运行,并且无法访问 private WiresharkFiles caps; 变量。请尝试以下操作:

private WiresharkFiles caps;
public WiresharkFiles Files { get { return caps; } }

有对应的

public MainWindow()
{
    InitializeComponent(); 
    caps = new WiresharkFiles();
    DataContext = Files;
}    

然后,您的 XAML 将按如下方式绑定到文件

IsEnabled="{Binding Path=HasItems}"

更新您需要查看按钮的实现和绑定命令,这将使它变得更好。 Look at this article for info on implementing and dealing with commands.

【讨论】:

  • 我需要把这个放在哪里: public WiresharkFiles Files { get { return caps; } } ?
  • 在大写的定义旁边就可以了。让我更新我的答案以显示它
  • 现在没有错误,但按钮已启用,尽管列表为空,我可以看到我的转换器甚至没有执行
  • "HasItems" 已经是布尔值了.. 为什么需要转换器?
  • @ShivaniKatukota 正在做某事,尝试在没有转换器的情况下运行它。在这种情况下,如果源类型和目标类型都是 bool,则不需要使用值转换器
【解决方案2】:

caps 是一个私有变量:

private WiresharkFiles caps;

为了绑定,它必须是公共属性:

public WiresharkFiles caps {get;set;}

您还必须将窗口的数据上下文设置为自身。类似的东西:

this.DataContext = this;


在你的窗口标签放:

 DataContext="{Binding RelativeSource={RelativeSource Self}}"

我不明白这与您最初的问题有何关系,但您可以在绑定中使用点符号。
你可以绑定:

{Binding AnObservableCollection.Count}

您可以将其与数据触发器中的 0 进行比较。如果你想禁用一个按钮和一个绑定命令,那么我会使用 icommand 的 canexecute 并返回 false 如果你没有条目或任何你的逻辑。

【讨论】:

  • 现在没有错误,但按钮已启用,尽管列表为空,我可以看到我的转换器甚至没有执行
  • Path=(caps.HasItems)在这里也是无效的(括号里的意思和OP想的不一样),应该是Path=caps.HasItems
  • 这就是我所拥有的:IsEnabled="{Binding Path=HasItems}"
  • 我可以将 this.DataContext = this 移动到 XAML 吗?
  • 可以将窗口的数据上下文绑定到自身。我在上面的帖子中添加了它。
猜你喜欢
  • 2013-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-23
  • 2011-01-10
  • 2011-12-14
  • 1970-01-01
相关资源
最近更新 更多