如何在不使用代码隐藏的情况下处理Keyboard.KeyDown事件?我们正在尝试使用MVVM模式,并避免在代码隐藏文件中编写事件处理程序.
我有一个自定义的UserControl DependencyProperty.当我从里面使用UserControl时DataTemplate,我无法设置值DependencyProperty.如果我直接在窗口中使用UserControl,那么DependencyProperty工作正常.我为长篇文章道歉,我将代码简化为最小化,仍然显示我在项目中遇到的问题.感谢您的帮助,我不知道还有什么可以尝试的.
主窗口XAML:
<Window ...>
<Window.Resources>
<DataTemplate DataType="{x:Type local:TextVM}">
<local:TextV MyText="I do not see this"/> <!--Instead I see "Default in Constructor"-->
</DataTemplate>
</Window.Resources>
<Grid>
<Border BorderThickness="5" BorderBrush="Black" Width="200" Height="100" >
<StackPanel>
<ContentControl Content="{Binding Path=TheTextVM}"/>
<local:TextV MyText="I see this"/>
</StackPanel>
</Border>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
主窗口代码:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
TheTextVM = new TextVM();
}
public TextVM TheTextVM { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
UserControl XAML:
<UserControl …Run Code Online (Sandbox Code Playgroud)