创建上下文绑定 XAML [英] Creating ContextBinding XAML
问题描述
我在为 stackPanel 内的所有字段设置 DataContext 时遇到问题.我想做的是将数据上下文设置为 vm:ViewModel.但它不起作用,当我向 VS 寻求 DataBinding 的帮助时,它会在 TextBox 内显示 TextBox.DataContext.有没有办法只设置一次,或者我必须为每个控件设置它?
I'm having problems setting the DataContext for all fields that are inside a stackPanel. What I would like to do is set the Data Context as vm:ViewModel. But it's not working and when I ask VS for assistance with DataBinding it Displays the TextBox.DataContext inside the TextBox. Is there a way to only set it once or do I have to set it for each control?
<StackPanel DataContext="vm:ViewModel">
<TextBox Text="{Binding FirstNumber}" HorizontalAlignment="Left" Height="23" Margin="206,45,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120">
<TextBox.DataContext>
<vm:ViewModel/>
</TextBox.DataContext>
</TextBox>
</StackPanel>
推荐答案
你需要让你的 DataContext 引用你的 ViewModel 的一个实例.
You need to get your DataContext to refer to an instance of your ViewModel.
DataContext="vm:ViewModel"
不是创建 ViewModel 的实例...它只是设置一个字符串.
DataContext="vm:ViewModel"
is not creating an instance of the ViewModel...it's just setting a string.
改用属性元素语法:
<StackPanel>
<StackPanel.DataContext>
<vm:ViewModel/>
</StackPanel.DataContext>
<TextBox Text="{Binding FirstNumber}" HorizontalAlignment="Left" Height="23" Margin="206,45,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
</StackPanel>
如果你这样做,那么就没有必要/事实上,在你的 TextBox.DataContext 中创建另一个可能是错误的......你只想继承 StackPanel 的 DataContext.
And if you do that, then there's no need/in fact it's probably wrong to create another one in your TextBox.DataContext...you just want to inherit the DataContext of the StackPanel.
我将向您展示另一种创建 ViewModel 并引用它的实例的方法,以便您有一个更大的了解.....那就是在资源中创建它,然后在资源中引用该资源绑定...这是一个例子:
I'll just show you another way to create a ViewModel and to refer to the instance of it so you have a bigger picture.....that is to create it in resources, and then refer to that resource in the binding...here's an example:
<Window x:Class="WpfApplication8.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:WpfApplication8"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<vm:ViewModel x:Key="myviewmodel"/>
</Window.Resources>
<StackPanel x:Name="stackp" DataContext="vm:ViewModel">
<TextBox Text="{Binding Source={StaticResource myviewmodel}, Path=FirstNumber}" HorizontalAlignment="Left" Height="23" Margin="206,45,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
</StackPanel>
</Window>
将 DataContext 设置在树中更高级别的另一个示例:
Another example that sets the DataContext at a higher level in the tree:
<Window x:Class="WpfApplication8.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:WpfApplication8"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<vm:ViewModel/>
</Window.DataContext>
<StackPanel>
<TextBox Text="{Binding FirstNumber}" HorizontalAlignment="Left" Height="23" Margin="206,45,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
</StackPanel>
</Window>
这篇关于创建上下文绑定 XAML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!