WP7 - 如何访问资源->样式中的按钮属性
我的 MainPage.xaml 中有这段代码:
<phone:PhoneApplicationPage.Resources>
<Style x:Key="ListBoxStyle" TargetType="ListBox">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/>
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled"/>
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="Padding" Value="0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBox">
<ScrollViewer x:Name="ScrollViewer" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Foreground="{TemplateBinding Foreground}" Padding="{TemplateBinding Padding}">
<StackPanel>
<ItemsPresenter/>
<Button x:Name="LoadMoreButton" Content="Load more data..." Background="{StaticResource PhoneAccentBrush}" Click="LoadMoreData_Click" Visibility="Collapsed" />
</StackPanel>
</ScrollViewer>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</phone:PhoneApplicationPage.Resources>
我的问题是如何访问 MainPage.xaml.cs 中的 LoadMoreButton.Visibility 属性?
如果我尝试使用 LoadMoreButton.Visibility intelisense 不会返回任何内容。我想您不能使用这样的资源元素,但我希望你们中的一些人知道这个“问题”的解决方案。先感谢您!
I have this piece of code in my MainPage.xaml :
<phone:PhoneApplicationPage.Resources>
<Style x:Key="ListBoxStyle" TargetType="ListBox">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/>
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled"/>
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="Padding" Value="0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBox">
<ScrollViewer x:Name="ScrollViewer" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Foreground="{TemplateBinding Foreground}" Padding="{TemplateBinding Padding}">
<StackPanel>
<ItemsPresenter/>
<Button x:Name="LoadMoreButton" Content="Load more data..." Background="{StaticResource PhoneAccentBrush}" Click="LoadMoreData_Click" Visibility="Collapsed" />
</StackPanel>
</ScrollViewer>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</phone:PhoneApplicationPage.Resources>
My question is how to access for example LoadMoreButton.Visibility property in MainPage.xaml.cs?
If I try to use LoadMoreButton.Visibility intelisense is not returning anything. I suppose that you cannot use resources elements like that but I hope so that some of you know solution of this "problem". Thank you in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
简短版本:你不能。
长版:你不能,我也不明白你为什么要这样做。在
ListBoxItem
内有一个命名按钮(意味着它被渲染多次)的整个想法是没有意义的。您真正想要做的是将可见性绑定到属性。考虑使用 ValueConverter(搜索 BoolToVisibilityConverter,您将找到),这样您就可以简单地从正在数据绑定的项目绑定布尔属性。
Short version: You can't.
Long version: You can't, and I don't see why you would. The whole idea about having a named button that's inside a
ListBoxItem
, meaning it's rendered several times, doesn't make sense.What you really want to do, is to bind the visibility to a property. Consider using a ValueConverter (Search for BoolToVisibilityConverter and thou shall find), so you can simply bind a boolean property from the item that's being databound.
使用此辅助函数:
找到您的按钮:
Use this helper function:
To locate your button: