我想将模型的方法绑定到ListViewItem模板中的按钮控件。如何在WPF中做到这一点?我尝试过,但是在运行时出现错误。
我只需要一种非常简单且正确的方法即可将方法绑定到按钮单击事件。 请帮帮我。我是WPF的新手。
这是我的代码.....
<ListView Grid.Row="1"
MaxHeight="500"
Name="entryLisView"
Grid.ColumnSpan="2">
<ListView.ItemTemplate>
<DataTemplate>
<Grid Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Text="New"
Margin="10,5,0,5"
VerticalAlignment="Top"
HorizontalAlignment="Left" />
<Button Name="cancelThisBtn"
Content="Delete"
Visibility="{Binding ItemCount, Mode=OneWay, Converter={StaticResource EduListItemCancelBtnVisibilityConverter}}"
Grid.Column="1"
Margin="0,5,10,5"
Click="{Binding DeleteDegree}"
VerticalAlignment="Top"
HorizontalAlignment="Right" />
<TextBox wpf:HintAssist.Hint="Exam or Degree Title"
Style="{DynamicResource MaterialDesignFloatingHintTextBox}"
Width="230"
FontSize="18"
Grid.Row="1"
Margin="10,0,10,0"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Text="{Binding Title, Mode=TwoWay}" />
<TextBox wpf:HintAssist.Hint="Subject"
Style="{DynamicResource MaterialDesignFloatingHintTextBox}"
Width="230"
FontSize="18"
Margin="10,0,10,0"
Grid.Row="1"
Grid.Column="1"
Text="{Binding SubjectName, Mode=TwoWay}"
VerticalAlignment="Center"
HorizontalAlignment="Center" />
<TextBox wpf:HintAssist.Hint="Institute Name"
Style="{DynamicResource MaterialDesignFloatingHintTextBox}"
Width="230"
FontSize="18"
Margin="10,20,10,0"
Grid.Row="2"
Grid.Column="0"
Text="{Binding InstituteName, Mode=TwoWay}"
VerticalAlignment="Center"
HorizontalAlignment="Center" />
<TextBox wpf:HintAssist.Hint="Passing Year"
Style="{DynamicResource MaterialDesignFloatingHintTextBox}"
Width="230"
FontSize="18"
Margin="10,20,10,0"
Grid.Row="2"
Grid.Column="1"
Text="{Binding PassingYear, Mode=TwoWay}"
VerticalAlignment="Center"
HorizontalAlignment="Center" />
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
这是我的模特
public class Degree : INotifyPropertyChanged
{
private int _id;
private string _title;
private string _instituteName;
private string _subjectName;
private string _passingYear;
private int _itemCount;
public string Title { get { return _title; } set { _title = value; RaisePropertyChanged(); } }
public string InstituteName { get { return _instituteName; } set { _instituteName = value; RaisePropertyChanged(); } }
public string SubjectName { get { return _subjectName; } set { _subjectName = value; RaisePropertyChanged(); } }
public string PassingYear { get { return _passingYear; } set { _passingYear = value; RaisePropertyChanged(); } }
public int Id { get { return _id; } set { _id = value; } }
public int ItemCount { get { return _itemCount; } set { _itemCount = value; RaisePropertyChanged(); } }
public Degree()
{
}
public void DeleteDegree(object sender, RoutedEventArgs e)
{
MessageBox.Show("I got u!");
}
//if (AddNewEducationDialog.Current != null)
//{
// AddNewEducationDialog.Current.degrees.Remove(this);
//}
public event PropertyChangedEventHandler PropertyChanged;
void RaisePropertyChanged([CallerMemberName]string name = null)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
}
答案 0 :(得分:2)
如果您使用数据绑定,请不要处理server:
max-http-header-size: 65536
事件。使用Click
,并将其绑定到查看模型的ICommand
属性:
Button.Command
XAML:
public class SomeVm
{
public SomeVm()
{
// initialize SomeCommand here;
// usually you need RelayCommand/DelegateCommand
SomeCommand = new RelayCommand(SomeMethod);
}
public ICommand SomeCommand { get; }
private void SomeMethod()
{
}
}
答案 1 :(得分:1)
您无法在MVVM中绑定方法。您需要改为使用命令。命令是由控件的默认操作行为触发的(例如,对于Button,默认触发器是Click)(如果要将命令绑定到默认事件以外的其他事件,则需要编写一些交互逻辑或使用支持该逻辑的库)。 / p>
以下是命令的示例:
private ICommand _ShowEntitiesCommand;
public ICommand ShowEntitiesCommmand
{
get
{
if (_ShowEntitiesCommand == null)
{
_ShowEntitiesCommand = new RelayCommand(ShowEntities);
}
return _ShowEntitiesCommand;
}
}
private void ShowEntities(object parameter)
{
SelectedViewModel = viewModelLocator.Get(parameter);
}
然后在按钮上仅设置Command属性: Command =“ {Binding ShowEntitiesCommmand}” CommandParameter =“ {Binding SomeParameterYoudLikeToPass}”
在这里,您可以检查实现ICommand的RelayCommand类的示例: https://github.com/Nidrax/Veritaware.Toolkits.LightVM/blob/master/Veritaware.Toolkits.LightVM.Net/RelayCommand.cs