【发布时间】:2014-07-11 22:37:26
【问题描述】:
我编写了一个示例来查看是否可以在空白的 Windows 应用商店应用程序中的 Style 中使用绑定 - 它已编译但没有完全按我希望的那样工作。我对 XAML 和绑定比较陌生,所以可能遗漏了一些东西。
在下面的示例中,有两个矩形,都绑定到滑块控件,并且都应该在滑块移动的同时发生变化,但似乎只有第一个发生变化;第一个直接绑定,第二个通过style绑定。
Style 中的绑定应该可以在 Win Store 应用程序中实现吗?
(我的目标是有一个滑块可以一次更改大量元素的设置,这似乎比复制/粘贴绑定到所有元素更好)
<Grid Background="#FF87873D">
<StackPanel>
<StackPanel.Resources>
<Style x:Key="myTestRectangleStyle" TargetType="Rectangle">
<Setter Property="Fill" Value="DarkBlue" />
<Setter Property="Margin" Value="10,10" />
<Setter Property="Height" Value="30" />
<Setter Property="Width" Value="{Binding ElementName=slider1, Path=Value}" />
</Style>
</StackPanel.Resources>
<Rectangle Width="{Binding ElementName=slider1, Path=Value}" Fill="Black" Margin="10,10" Height="30"/>
<Rectangle Style="{StaticResource myTestRectangleStyle}"/>
<Slider Name="slider1" Minimum="20" Maximum="200" Margin="20,0"/>
</StackPanel>
</Grid>
【问题讨论】:
-
尝试设置
DataContext,以便绑定知道它正在查找可视树的类型。 -
感谢您的回复!我对
DataContext进行了一些阅读,我尝试了一些关于在哪里设置但没有改变的选项,例如在<Setter Property="Height" Value="30" />之后,我尝试了<Setter Property="DataContext" Value="{Binding ElementName=slider1}" /><Setter Property="Width" Value="{Binding Path=DataContext.Value}" />。我是否将 DataContext 添加到正确的位置?
标签: c# xaml windows-store-apps xaml-binding