我遇到的问题是让我的按钮使用绑定。我不确定要设置DataContext的内容。我的绑定出现了这个错误:
System.Windows.Data Error: 40 : BindingExpression path error: 'ClickMeCommand' property not found on 'object' ''Person' (HashCode=13029119)'. BindingExpression:Path=ClickMeCommand; DataItem='Person' (HashCode=13029119); target element is 'Button' (Name=''); target property is 'Command' (type 'ICommand')
我在这里做的是我在XAML中有一个ListView,我有一个有三列的网格视图。一列存储Person对象中的Name,第二列存储Person对象中的Date,第三列对于列表中的所有内容都是相同的,一个按钮。单击该按钮时,我想执行一个命令从我的observable集合中删除该任务并更新列表。我知道怎么做但我无法获得按钮的绑定命令。
<ListView x:Name="List" HorizontalAlignment="Left" Margin="0,0,0,-2"` Width="292" HorizontalContentAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Disabled" Height="Auto">
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView>
<GridView.ColumnHeaderContainerStyle>
<Style BasedOn="{StaticResource {x:Type GridViewColumnHeader}}" TargetType="{x:Type GridViewColumnHeader}">
<Setter Property="IsHitTestVisible" Value="False"/>
</Style>
</GridView.ColumnHeaderContainerStyle>
<GridViewColumn x:Name="Column1" Header="Assignment" DisplayMemberBinding="{Binding Name}" Width="190"/>
<GridViewColumn x:Name="Column2" Header="Date" DisplayMemberBinding="{Binding Date}" Width="45"/>
<GridViewColumn Header="Edit" Width="35" CellTemplate="{StaticResource editColumnTemplate}">
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
<Window.Resources>
<DataTemplate x:Key="editColumnTemplate">
<StackPanel Orientation="Horizontal">
<Button x:Name="deleteButton" Content="X" Command="{Binding Path=ClickMeCommand}" HorizontalAlignment="Stretch"/>
</StackPanel>
</DataTemplate>
</Window.Resources>
//
private class Person
{
public String Name { get; set; }
public String Date { get; set; }
}
在我的代码中,我创建了一个新选项卡并在其中添加了一个列表视图。每次我添加一个新人时,它会在列表中添加一行,其中包含他/她的姓名/日期和第三列上的按钮。
public MainWindow(){
InitializeComponent();
this.DataContext = new MainViewModel();
}
TabItem newItem = new TabItem();
ListView newList = new ListView();
ObservableCollection<TabItem> newSource = new ObservableCollection<TabItem>();
newList.ItemsSource = newSource;
TabControl.Items.Add(newItem);
从上面可以看到,该按钮绑定到命令ClickMeCommand。这是班级:
public class MainViewModel
{
public MainViewModel()
{
}
private ICommand clickMeCommand;
public ICommand ClickMeCommand
{
get
{
if (clickMeCommand == null)
clickMeCommand = new RelayCommand(i => this.ClickMe(), null);
return clickMeCommand;
}
}
private void ClickMe()
{
MessageBox.Show("You Clicked Me");
}
}
public class RelayCommand : ICommand
{
readonly Action<object> execute;
readonly Predicate<object> canExecute;
public RelayCommand(Action<object> executeDelegate, Predicate<object> canExecuteDelegate)
{
execute = executeDelegate;
canExecute = canExecuteDelegate;
}
bool ICommand.CanExecute(object parameter)
{
return canExecute == null ? true : canExecute(parameter);
}
event EventHandler ICommand.CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
void ICommand.Execute(object parameter)
{
execute(parameter);
}
}