gpt4 book ai didi

c# - wpf InvokeCommandAction 绑定(bind)

转载 作者:太空宇宙 更新时间:2023-11-03 13:09:36 24 4
gpt4 key购买 nike

我在绑定(bind)方面遇到了一些问题。我不知道该怎么做。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GalaSoft.MvvmLight;
using ManyViews.Model;
using System.Collections.ObjectModel;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;

namespace ManyViews.ViewModel
{
class ListViewModel : ViewModelBase
{
public void show()
{
System.Windows.MessageBox.Show("Raise");
}
public ListViewModel()
{
EventChecked = new RelayCommand(() => show());
}
public ICommand EventChecked { get; set; }
public ObservableCollection<ListStruct> ShowList
{
get { return ListM.Items; }
set { ListM.Items = value; RaisePropertyChanged("ShowList"); }
}
ListModel ListM = new ListModel();
}
}

如果我需要选择另一个上下文,我总是使用静态资源,但在这种情况下,它在 EventTrigger 中没有属性数据上下文。

<UserControl x:Class="ManyViews.View.List"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:test="clr-namespace:ManyViews.ViewModel"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<test:ListViewModel x:Key="Test"></test:ListViewModel>
</UserControl.Resources>
<Grid>
<ListView ItemsSource="{Binding ShowList}">
<ListView.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding name}">
<i:Interaction.Triggers >
<i:EventTrigger EventName="Checked">
<i:InvokeCommandAction Command="{Binding DataContext.EventChecked, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type test:ListViewModel}}}"></i:InvokeCommandAction>
</i:EventTrigger>
</i:Interaction.Triggers>
</CheckBox>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>

在这种情况下我有错误

"System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='ManyViews.ViewModel.ListViewModel', AncestorLevel='1''. BindingExpression:Path=DataContext.EventChecked; DataItem=null; target element is 'InvokeCommandAction' (HashCode=43550996); target property is 'Command' (type 'ICommand')"

最佳答案

AncestorType={x:Type test:ListViewModel} 是你的问题。

可视化树中没有比当前项目更高的test:ListViewModel。只有不同的控件,没有( View )模型。

将其更改为搜索 ListView:

<i:InvokeCommandAction  
Command="{Binding DataContext.EventChecked, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListView}}}">
</i:InvokeCommandAction>

关于c# - wpf InvokeCommandAction 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29578146/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com