找不到文本框键绑定
TextBox KeyBinding not found
public MainWindow() {
insertNewScoreCommand = new SimpleDelegateCommand(x => this.InsertNewScore(x));
insertNewScoreCommand.GestureKey = Key.Enter;
insertNewScoreCommand.GestureModifier = ModifierKeys.Control;
insertNewScoreCommand.MouseGesture = MouseAction.RightClick;
}
public SimpleDelegateCommand InsertNewScoreCommand {
get { return insertNewScoreCommand; }
}
private SimpleDelegateCommand insertNewScoreCommand;
private void InsertNewScore(object sender) { }
我在 DataGrip 中有一个输入字段,我想用它来将新分数插入数据库。我的想法是当我在字段中按下 Enter 时触发一个方法。
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Path=Disciplines}" HeadersVisibility="None" CanUserAddRows="False" CanUserDeleteRows="False" Margin="0" BorderThickness="0">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=DisciplineId}" IsReadOnly="True" Visibility="Hidden" />
<DataGridTextColumn Binding="{Binding Path=Name }" IsReadOnly="True" Width="70"/>
<DataGridTemplateColumn Width="*" MinWidth="52">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox MinWidth="50" >
<TextBox.InputBindings>
<KeyBinding Key="Return" Command="{Binding InsertNewScoreCommand}" />
</TextBox.InputBindings>
</TextBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
但是WPF一直说绑定不存在。这是我第一次做 KeyBinding,我不明白哪里出了问题。我以为我已经完成了文档中的所有内容。
System.Windows.Data 错误:40:BindingExpression 路径错误:在 'object' ''Discipline_A320C92756365475DEC4BDC8039368751EFACDCE5B74C1FE572674EA0C24C666' (HashCode=39958021)' 上找不到 'InsertNewScoreCommand' 属性。 BindingExpression:Path=InsertNewScoreCommand; DataItem='Discipline_A320C92756365475DEC4BDC8039368751EFACDCE5B74C1FE572674EA0C24C666' (哈希码=39958021);目标元素是 'KeyBinding' (HashCode=18759866);目标 属性 是 'Command'(类型 'ICommand')
绑定将在当前绑定上下文中查找指定的绑定路径。默认情况下,它将是当前 DataContext。您可以使用 ElementName、Source 或 RelativeSource 更改绑定上下文。在您的情况下,假设您的 InsertNewScoreCommand 和 DataGrid DataContext 在同一个 ViewModel 中,您可以使用您的 DataGrid DataContext。
<DataGrid x:Name="disciplinesDataGrid" AutoGenerateColumns="False" ItemsSource="{Binding Path=Disciplines}" HeadersVisibility="None" CanUserAddRows="False" CanUserDeleteRows="False" Margin="0" BorderThickness="0">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=DisciplineId}" IsReadOnly="True" Visibility="Hidden" />
<DataGridTextColumn Binding="{Binding Path=Name }" IsReadOnly="True" Width="70"/>
<DataGridTemplateColumn Width="*" MinWidth="52">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox MinWidth="50" >
<TextBox.InputBindings>
<KeyBinding Key="Return" Command="{Binding ElementName=disciplinesDataGrid, Path=InsertNewScoreCommand}" />
</TextBox.InputBindings>
</TextBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
public MainWindow() {
insertNewScoreCommand = new SimpleDelegateCommand(x => this.InsertNewScore(x));
insertNewScoreCommand.GestureKey = Key.Enter;
insertNewScoreCommand.GestureModifier = ModifierKeys.Control;
insertNewScoreCommand.MouseGesture = MouseAction.RightClick;
}
public SimpleDelegateCommand InsertNewScoreCommand {
get { return insertNewScoreCommand; }
}
private SimpleDelegateCommand insertNewScoreCommand;
private void InsertNewScore(object sender) { }
我在 DataGrip 中有一个输入字段,我想用它来将新分数插入数据库。我的想法是当我在字段中按下 Enter 时触发一个方法。
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Path=Disciplines}" HeadersVisibility="None" CanUserAddRows="False" CanUserDeleteRows="False" Margin="0" BorderThickness="0">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=DisciplineId}" IsReadOnly="True" Visibility="Hidden" />
<DataGridTextColumn Binding="{Binding Path=Name }" IsReadOnly="True" Width="70"/>
<DataGridTemplateColumn Width="*" MinWidth="52">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox MinWidth="50" >
<TextBox.InputBindings>
<KeyBinding Key="Return" Command="{Binding InsertNewScoreCommand}" />
</TextBox.InputBindings>
</TextBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
但是WPF一直说绑定不存在。这是我第一次做 KeyBinding,我不明白哪里出了问题。我以为我已经完成了文档中的所有内容。
System.Windows.Data 错误:40:BindingExpression 路径错误:在 'object' ''Discipline_A320C92756365475DEC4BDC8039368751EFACDCE5B74C1FE572674EA0C24C666' (HashCode=39958021)' 上找不到 'InsertNewScoreCommand' 属性。 BindingExpression:Path=InsertNewScoreCommand; DataItem='Discipline_A320C92756365475DEC4BDC8039368751EFACDCE5B74C1FE572674EA0C24C666' (哈希码=39958021);目标元素是 'KeyBinding' (HashCode=18759866);目标 属性 是 'Command'(类型 'ICommand')
绑定将在当前绑定上下文中查找指定的绑定路径。默认情况下,它将是当前 DataContext。您可以使用 ElementName、Source 或 RelativeSource 更改绑定上下文。在您的情况下,假设您的 InsertNewScoreCommand 和 DataGrid DataContext 在同一个 ViewModel 中,您可以使用您的 DataGrid DataContext。
<DataGrid x:Name="disciplinesDataGrid" AutoGenerateColumns="False" ItemsSource="{Binding Path=Disciplines}" HeadersVisibility="None" CanUserAddRows="False" CanUserDeleteRows="False" Margin="0" BorderThickness="0">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=DisciplineId}" IsReadOnly="True" Visibility="Hidden" />
<DataGridTextColumn Binding="{Binding Path=Name }" IsReadOnly="True" Width="70"/>
<DataGridTemplateColumn Width="*" MinWidth="52">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox MinWidth="50" >
<TextBox.InputBindings>
<KeyBinding Key="Return" Command="{Binding ElementName=disciplinesDataGrid, Path=InsertNewScoreCommand}" />
</TextBox.InputBindings>
</TextBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>