WP7 - 如何访问资源->样式中的按钮属性

发布于 2024-11-29 21:23:16 字数 1905 浏览 7 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

俏︾媚 2024-12-06 21:23:16

简短版本:你不能。

长版:你不能,我也不明白你为什么要这样做。在 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.

独享拥抱 2024-12-06 21:23:16

使用此辅助函数:

        public static ChildItem FindVisualChild<ChildItem>(DependencyObject obj)
            where ChildItem : DependencyObject {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++) {
                DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                if (child != null && child is ChildItem) {
                    return (ChildItem)child;
                } else {
                    ChildItem childOfChild = FindVisualChild<ChildItem>(child);
                    if (childOfChild != null) {
                        return childOfChild;
                    }
                }
            }
            return null;
        }

找到您的按钮:

var button = FindVisualChild<Button>(listbox);

Use this helper function:

        public static ChildItem FindVisualChild<ChildItem>(DependencyObject obj)
            where ChildItem : DependencyObject {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++) {
                DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                if (child != null && child is ChildItem) {
                    return (ChildItem)child;
                } else {
                    ChildItem childOfChild = FindVisualChild<ChildItem>(child);
                    if (childOfChild != null) {
                        return childOfChild;
                    }
                }
            }
            return null;
        }

To locate your button:

var button = FindVisualChild<Button>(listbox);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文