WPF:来自MainWindow.xaml的绑定属性导致错误

时间:2018-04-13 10:58:36

标签: wpf binding

所以我有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)); } }

private WiresharkFiles caps;

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

MainWindow.xaml

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

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

我得到了这个{{1}}:

  

XamlParseException:类型引用找不到命名的类型   &#39; {LabelEncoder}帽&#39;

2 个答案:

答案 0 :(得分:0)

我看不到您将'data': 'indoor_3D_map_example.geojson' DataContext属性相关联的位置。

确保您拥有公共属性,因为WPF引擎未在您的类中运行,并且无法访问caps变量。请尝试以下方法:

private WiresharkFiles caps;

带有相应的

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

然后您的XAML将绑定到文件,如下所示

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

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

答案 1 :(得分:0)

大写是一个私有变量:

private WiresharkFiles caps;

为了绑定,它必须是公共财产:

public WiresharkFiles caps {get;set;}

您还必须将窗口的datacontext设置为自身。类似的东西:

this.DataContext = this;


在你的窗口标签中:

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

我不知道这与您的初始问题有什么关系,但您可以在绑定中使用点符号 你可以绑定:

{Binding AnObservableCollection.Count}

您可以在数据触发器中将其与0进行比较。如果要禁用它,可以使用按钮和绑定命令,然后使用icommand的canexecute,如果没有条目或任何逻辑,则返回false。