使用属性更改简单绑定到用户控件在vs扩展工具窗口中不起作用



我有一个UserControl在Vs扩展,我想创建一个属性绑定但是由于某些原因它不起作用

在xaml

<UserControl x:Class="RemoteDebuggerToolBarExtension.Windows.CommandRunnerControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:vsshell="clr-namespace:Microsoft.VisualStudio.Shell;assembly=Microsoft.VisualStudio.Shell.15.0"
xmlns:windows="clr-namespace:RemoteDebuggerToolBarExtension.Windows"
Background="{DynamicResource {x:Static vsshell:VsBrushes.WindowKey}}"
Foreground="{DynamicResource {x:Static vsshell:VsBrushes.WindowTextKey}}"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
d:DataContext="{d:DesignInstance windows:CommandRunnerControl}"
Name="MyToolWindow">
<Grid>
<StackPanel Orientation="Vertical">
<TextBlock Margin="10" >Type your command here:</TextBlock>
<TextBox Margin="10" Width="200" TextWrapping="WrapWithOverflow" Text="{Binding CommandData,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged }"></TextBox>
<Button Margin="10" Content="Run" Click="RunRemoteCommand"  Name="button1" />
</StackPanel>
</Grid>

背后的代码
namespace RemoteDebuggerToolBarExtension.Windows
public partial class CommandRunnerControl : UserControl, INotifyPropertyChanged
{
private string _commandData = string.Empty;
public event PropertyChangedEventHandler PropertyChanged;
public string CommandData
{
get => _commandData;
set
{
_commandData = value;
OnPropertyChanged(nameof(CommandData));
}
}
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}

在没有更新的情况下输入文本我错过了什么?

设置DataContext或显式指定绑定源:

<TextBox Margin="10" Width="200" TextWrapping="WrapWithOverflow"
Text="{Binding CommandData,UpdateSourceTrigger=PropertyChanged, 
RelativeSource={RelativeSource AncestorType=UserControl} }" />

最新更新