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

时间:2011-05-11 07:10:12

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

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

我该怎么办?

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

1 个答案:

答案 0 :(得分: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));
        }
  }
}