I have a UserControl in Vs extension, I want to make a Property binding but from some reason it not works
the 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>
Code behind
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));
}
}
while typing text inside nothing updates what do i miss?
CodePudding user response:
Set the DataContext
or specify the source of the binding explicitly:
<TextBox Margin="10" Width="200" TextWrapping="WrapWithOverflow"
Text="{Binding CommandData,UpdateSourceTrigger=PropertyChanged,
RelativeSource={RelativeSource AncestorType=UserControl} }" />