【问题标题】:How to change the visibility of a template button in a listbox?如何更改列表框中模板按钮的可见性?
【发布时间】:2011-05-11 07:10:12
【问题描述】:

我正在创建一个 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> 

【问题讨论】:

  • 你想什么时候隐藏按钮?何时选择特定项目或取决于某些条件?另外关于您提到的属性 - 它是 DataSource 的属性还是当前页面的属性?
  • 我使用枢轴控件,当用户切换到另一个枢轴项目时,如果列表没有任何值,我希望隐藏按钮。我在当前页面上创建了一个属性,并尝试将其绑定到按钮的 Visibility 属性。
  • Visibility = {Binding path = YourProperty}" 应该可以工作。确保您正确设置所有(DataContext + Property)
  • 我将我的页面的 x:name 定义为“HomePage”并添加了这个:Visibility="{ Binding MoreButtonVisibility, ElementName=HomePage}" 但是只有当我设置 MoreButtonVisibility 的值时在页面构造函数中定义它,但当我从事件方法定义它时没有

标签: c# wpf windows-phone-7 silverlight-3.0 binding


【解决方案1】:

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

如果您使用的是 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));
        }
  }
}

【讨论】:

    猜你喜欢
    • 2019-08-14
    • 2014-08-04
    • 1970-01-01
    • 2011-01-12
    • 2016-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-05
    相关资源
    最近更新 更多