Как привязать к keybinding.command к viewmodel, когда у меня textbox уже привязан к models. Мне нужно чтобы по нажатию Enter создавался новый элемент
Как привязать к keybinding.command к viewmodel, когда у меня textbox уже привязан к models. Мне нужно чтобы по нажатию Enter создавался новый элемент
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="using:Testing_ItemsControl.ViewModels"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:models="clr-namespace:Testing_ItemsControl.Models"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="Testing_ItemsControl.Views.MainWindow"
x:DataType="vm:MainWindowViewModel"
Icon="/Assets/avalonia-logo.ico"
Title="Testing_ItemsControl">
<Design.DataContext>
<!-- This only sets the DataContext for the previewer in an IDE,
to set the actual DataContext for runtime, set the DataContext property in code (look at App.axaml.cs) -->
<vm:MainWindowViewModel />
</Design.DataContext>
<StackPanel Margin="20">
<TextBlock Margin="0 5">List of crockery:</TextBlock>
<ItemsControl ItemsSource="{Binding CrockeryList}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border Margin="0,10,0,0"
CornerRadius="5"
BorderBrush="Gray" BorderThickness="1"
Padding="5">
<StackPanel Orientation="Horizontal">
<TextBox Text="{Binding Path=Number, Mode=TwoWay}">
<TextBox.KeyBindings>
<KeyBinding Gesture="Enter" Command="{Binding Source={}}"></KeyBinding>
</TextBox.KeyBindings>
</TextBox>
<!-- <TextBox Text="{Binding DataType=models:Crockery, Path=(models:Crockery).Title}"></TextBox> -->
</StackPanel>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<Button Content="Create new item" Command="{Binding CreateItem}">
</Button>
</StackPanel>
</Window>
Вот models
namespace Testing_ItemsControl.Models;
public class Crockery
{
public string Title { get; set; }
public int Number{ get; set; }
public Crockery(string title, int number)
{
Title = title;
Number = number;
}
}
вот viewmodels
public class MainWindowViewModel : ViewModelBase
{
public ObservableCollection<Crockery> CrockeryList { get; set; }
public ReactiveCommand <Unit, Unit> CreateItem { get; }
public MainWindowViewModel()
{
CrockeryList = new ObservableCollection<Crockery>(new List<Crockery>());
CreateItem = ReactiveCommand.Create(() =>
{
CrockeryList.Add(new Crockery("sad", 22));
});
}
}