gpt4 book ai didi

c# - 可以在样式内的 XAML 中使用绑定(bind)吗?

转载 作者:太空宇宙 更新时间:2023-11-03 15:53:40 26 4
gpt4 key购买 nike

我写了一个示例来查看是否可以在空白 Windows 应用商店应用程序的 Style 中使用绑定(bind) - 它已编译但没有完全按照我希望的那样工作。我对 XAML 和绑定(bind)比较陌生,所以可能错过了一些东西。

在下面的示例中有两个矩形,都绑定(bind)到 slider 控件,并且都应该在 slider 移动的同时发生变化,但似乎只有第一个发生变化;第一个是直接绑定(bind),第二个是通过 style 绑定(bind)。

Style 中的绑定(bind)是否应该在 Win Store 应用程序中可行?(我的目标是有一个 slider 可以同时更改大量元素的设置,这似乎比复制/粘贴所有元素的绑定(bind)更好)

<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>

最佳答案

回答我自己的问题...似乎这在 Windows 应用商店应用程序上是不可能的。

我在 MSDN forum 上得到了用户的澄清那个

[Bindings] are not supported on Style setters in Windows Store Apps like they are in WPF, i.e. you cannot bind to the Value property of the Slider in the Style

所以解决方法就是直接在 Style 之外设置绑定(bind)(不幸的是,如果你有很多元素要绑定(bind),这是一个冗长的选项)

关于c# - 可以在样式内的 XAML 中使用绑定(bind)吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24707640/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com