Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF:MainWindow.xaml中的bind属性导致错误_Wpf_Binding - Fatal编程技术网

WPF:MainWindow.xaml中的bind属性导致错误

WPF:MainWindow.xaml中的bind属性导致错误,wpf,binding,Wpf,Binding,所以我有了这个视图模型: public class WiresharkFiles : INotifyPropertyChanged { public ObservableCollection<WiresharkFile> List { get; set; } public event PropertyChangedEventHandler PropertyChanged; private bool _inUse; private int _packet

所以我有了这个
视图模型

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));
    }
}
窗口资源

<Window.Resources>
   <Convertors:CollectionHasItemsConverter x:Key="CollectionHasItemsConverter"/>
</Window.Resources>
我想启用/禁用我的
按钮

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

我得到了这个
错误

XamlParseException:类型引用找不到名为 “{}caps”


我看不出您在哪里将
DataContext
caps
属性相关联

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

private WiresharkFiles caps;
public WiresharkFiles Files { get { return caps; } }
有相应的

public MainWindow()
{
    InitializeComponent(); 
    caps = new WiresharkFiles();
    DataContext = Files;
}    
然后,您的XAML将绑定到以下文件

IsEnabled="{Binding Path=HasItems}"

更新您需要了解如何实现和绑定按钮的命令,这将使它变得更好

caps是一个私有变量:

private WiresharkFiles caps;
为了具有约束力,它必须是公共财产:

public WiresharkFiles caps {get;set;}
您还必须将窗口的datacontext设置为自身。比如:

this.DataContext = this;

在您的窗口标签中放置:

 DataContext="{Binding RelativeSource={RelativeSource Self}}"
我看不出这与您最初的问题有什么关系,但您可以在绑定中使用点符号。
您可以绑定:

{Binding AnObservableCollection.Count}

您可以将其与datatrigger中的0进行比较。使用按钮和绑定命令,如果您想禁用它,那么我将使用icommand的canexecute,如果您没有条目或您的逻辑是什么,则返回false。

我需要将以下内容放在哪里:public WiresharkFiles Files{get{return caps;}}?放在caps定义旁边就可以了。现在让我更新我的答案以显示无错误,但是按钮是启用的,尽管列表为空,我可以看到我的转换器甚至没有执行“HasItems”已经是布尔值。。你为什么需要转换器?@ShivaniKatukota发现了一些东西,试着在没有转换器的情况下运行它。在这种情况下,如果源类型和目标类型都是
bool
,则无需使用值转换器。此外,caps是一个私有变量,且未设置数据上下文,绑定中的圆括号是问题所在。现在没有错误,但按钮是启用的,尽管列表为空,我可以看到我的转换器甚至没有执行
Path=(caps.HasItems)
在这里也是无效的(括号的含义与OP所想的不同),它应该是
Path=caps.HasItems
,这就是我所拥有的:IsEnabled=“{Binding Path=HasItems}”我可以将This.DataContext=This移动到XAML吗?您可以将窗口的DataContext绑定到自身。我在上面的帖子里加了这个。
 DataContext="{Binding RelativeSource={RelativeSource Self}}"
{Binding AnObservableCollection.Count}