通过 Viewmodel 和 Binding 访问 TextBox
Accessing a TextBox through Viewmodel and Binding
我创建了一个 C# WPF 程序,其中有一个 TextBox
。我希望此 TextBox
使用 viewmodel
提供事件反馈。
简化示例:单击按钮时,TextBox
显示 "...Button clicked"
。
我目前在后面的代码中有它:
public partial class MainWindow : Window
{
//.....
public void FeedbackPanel(string text)
{
if (FeedbkPanelTextBox != null)
{
if (text != null)
{
FeedbkPanelTextBox.AppendText(text + "\n");
}
else
{
FeedbkPanelTextBox.AppendText("Null\n");
}
}
else
{
return;
}
}
}
我如何将此代码移到 viewmodel
并在 view
中使用绑定?
已编辑
快速示例:
<Window x:Class="ButtonClickedFeedbackICommand.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ButtonClickedFeedbackICommand"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.DataContext>
<local:ViewModel/>
</Grid.DataContext>
<StackPanel Orientation="Horizontal">
<TextBox x:Name="tbFeedback"
Text="{Binding ClickedFeedback}"
MinWidth="50"
Background="SlateGray"
VerticalAlignment="Center"/>
<Button Content="Click"
Command="{Binding TestCommand}"
CommandParameter="{Binding ElementName=tbFeedback, Path=Text}"
VerticalAlignment="Center" />
</StackPanel>
</Grid>
这是您的视图。为了支持您所说的,我们需要一种与其他 class 进行通信的方式。我们的按钮将使用一个命令和一个 CommandParameter,这将利用对 TextBox 的 Text 属性 的访问。
这是您的简单 ViewModel:
public class ViewModel
{
public ICommand TestCommand { get; set; }
public ViewModel()
{
TestCommand = new TestCommand(this);
}
public void FeedbackPanel(string text)
{
if (text != null)
{
if (text != null)
{
text += (text + "\n");
}
else
{
text += ("Null\n");
}
}
else
{
return;
}
}
}
}
以及命令:
public class TestCommand : ICommand
{
public ViewModel _vm { get; set; }
public TestCommand(ViewModel vm)
{
_vm = vm;
}
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
_vm.FeedbackPanel(parameter.ToString());
}
}
您可以选择在该 CommandParameter 中发送其他内容。在认为流程尊重您的需求。随意玩一会儿。
我创建了一个 C# WPF 程序,其中有一个 TextBox
。我希望此 TextBox
使用 viewmodel
提供事件反馈。
简化示例:单击按钮时,TextBox
显示 "...Button clicked"
。
我目前在后面的代码中有它:
public partial class MainWindow : Window
{
//.....
public void FeedbackPanel(string text)
{
if (FeedbkPanelTextBox != null)
{
if (text != null)
{
FeedbkPanelTextBox.AppendText(text + "\n");
}
else
{
FeedbkPanelTextBox.AppendText("Null\n");
}
}
else
{
return;
}
}
}
我如何将此代码移到 viewmodel
并在 view
中使用绑定?
已编辑
快速示例:
<Window x:Class="ButtonClickedFeedbackICommand.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ButtonClickedFeedbackICommand"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.DataContext>
<local:ViewModel/>
</Grid.DataContext>
<StackPanel Orientation="Horizontal">
<TextBox x:Name="tbFeedback"
Text="{Binding ClickedFeedback}"
MinWidth="50"
Background="SlateGray"
VerticalAlignment="Center"/>
<Button Content="Click"
Command="{Binding TestCommand}"
CommandParameter="{Binding ElementName=tbFeedback, Path=Text}"
VerticalAlignment="Center" />
</StackPanel>
</Grid>
这是您的视图。为了支持您所说的,我们需要一种与其他 class 进行通信的方式。我们的按钮将使用一个命令和一个 CommandParameter,这将利用对 TextBox 的 Text 属性 的访问。
这是您的简单 ViewModel:
public class ViewModel
{
public ICommand TestCommand { get; set; }
public ViewModel()
{
TestCommand = new TestCommand(this);
}
public void FeedbackPanel(string text)
{
if (text != null)
{
if (text != null)
{
text += (text + "\n");
}
else
{
text += ("Null\n");
}
}
else
{
return;
}
}
}
}
以及命令:
public class TestCommand : ICommand
{
public ViewModel _vm { get; set; }
public TestCommand(ViewModel vm)
{
_vm = vm;
}
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
_vm.FeedbackPanel(parameter.ToString());
}
}
您可以选择在该 CommandParameter 中发送其他内容。在认为流程尊重您的需求。随意玩一会儿。