如何更改列表框中模板按钮的可见性?

发布于 2024-11-06 05:20:01 字数 2879 浏览 0 评论 0原文

我正在创建 Windows Phone 应用程序,但列表框模板有问题。 我想在运行时隐藏 MoreListBoxStyle 中定义的“MoreButton”。 我尝试创建一个属性并将其绑定到按钮的可见性属性,但它不起作用。

我该怎么办?

<phone:PhoneApplicationPage.Resources>
    <Style x:Key="MoreListBoxStyle" 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="MoreButton"   Content="{Binding Path=LocaleResources.More, Source={StaticResource LocalizedStrings}}" Height="67" Margin="0,0,8,0" BorderBrush="{x:Null}" Foreground="{StaticResource PhoneForegroundBrush}" BorderThickness="0" FontSize="17" FontWeight="Bold" Click="MoreButton_Click"  />
                        </StackPanel>
                    </ScrollViewer>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</phone:PhoneApplicationPage.Resources>

我的列表框是:

<ListBox x:Name="RandomListBox" ItemsSource="{Binding}" Grid.Row="1" Style="{StaticResource MoreListBoxStyle}">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Vertical">
                                <TextBlock Text="{Binding MyText}" TextWrapping="Wrap" Width="440" Margin="0,10"   Name="{Binding MyId}" ManipulationCompleted="TextBlock_ManipulationCompleted" />
                                <TextBlock Text="{Binding Name}" Width="440" TextWrapping="Wrap" TextAlignment="Right" Margin="0,0,0,15" />
                                <Rectangle Width="440" Height="3" Fill="{StaticResource PhoneForegroundBrush}"></Rectangle>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>

                </ListBox> 

I am creating a Windows Phone application and I have a problem with a listbox template.
I would like to hide the "MoreButton" defined in the MoreListBoxStyle at runtime.
I tried to create a property and bind it to the visibility property of the button but it doesn't work.

How should I do ?

<phone:PhoneApplicationPage.Resources>
    <Style x:Key="MoreListBoxStyle" 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="MoreButton"   Content="{Binding Path=LocaleResources.More, Source={StaticResource LocalizedStrings}}" Height="67" Margin="0,0,8,0" BorderBrush="{x:Null}" Foreground="{StaticResource PhoneForegroundBrush}" BorderThickness="0" FontSize="17" FontWeight="Bold" Click="MoreButton_Click"  />
                        </StackPanel>
                    </ScrollViewer>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</phone:PhoneApplicationPage.Resources>

And my listbox is :

<ListBox x:Name="RandomListBox" ItemsSource="{Binding}" Grid.Row="1" Style="{StaticResource MoreListBoxStyle}">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Vertical">
                                <TextBlock Text="{Binding MyText}" TextWrapping="Wrap" Width="440" Margin="0,10"   Name="{Binding MyId}" ManipulationCompleted="TextBlock_ManipulationCompleted" />
                                <TextBlock Text="{Binding Name}" Width="440" TextWrapping="Wrap" TextAlignment="Right" Margin="0,0,0,15" />
                                <Rectangle Width="440" Height="3" Fill="{StaticResource PhoneForegroundBrush}"></Rectangle>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>

                </ListBox> 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

甜宝宝 2024-11-13 05:20:01

据我了解您的问题,我认为您有两个选择:

如果您使用的是 CLR 属性,请确保您已实现 INotifyPropertyChanged ,例如:

public partial class MainPage : PhoneApplicationPage, INotifyPropertyChanged
{
       ...
    Visibility sampleProperty;
    public Visibility SampleProperty
    {
        get
        {
            return sampleProperty;
        }
        set
        {
            sampleProperty = value;
            // Call OnPropertyChanged whenever the property is updated
            OnPropertyChanged("SampleProperty");
        }

    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
  }
}

As far as I understand your question I think that you have two options:

If you are using a CLR property than make sure that you have implemented INotifyPropertyChanged like for example:

public partial class MainPage : PhoneApplicationPage, INotifyPropertyChanged
{
       ...
    Visibility sampleProperty;
    public Visibility SampleProperty
    {
        get
        {
            return sampleProperty;
        }
        set
        {
            sampleProperty = value;
            // Call OnPropertyChanged whenever the property is updated
            OnPropertyChanged("SampleProperty");
        }

    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文