【问题标题】:How to make a button programatically visible in windows phone?如何使按钮以编程方式在 Windows Phone 中可见?
【发布时间】:2013-10-25 06:41:08
【问题描述】:

我有一个名为删除的按钮。 我只想在满足某些条件时才可见, 那我该怎么做呢??

用于创建按钮的 XAML 代码是

<Button x:Name="DeleteButton" Content="Delete" HorizontalAlignment="Left" Height="64" Margin="74,579,0,-9" VerticalAlignment="Top" Width="314" FontSize="24"/>

【问题讨论】:

    标签: c# button binding windows-phone


    【解决方案1】:

    你有一个可见性属性。

    你有一些方法可以做到:

    1. 就在你后面的代码中:

      if (condition)
      {
          DeleteButton.Visibility = Visibility.Visible; //Also possible to Collapse (hide).
      }
      

      上面的代码应该可以帮助您分别使按钮不可见和可见。

      注意:这是不太可取的,它不是动态的,可能会导致重复和不必要的代码。

    2. 更好的方式和更动态的是:

      您可以创建一个 bool 属性并将可见性按钮绑定到它,如下所示:

      bool IsVisible { get; set; } //Code behind
      

      在 xaml 中:

      <!-- Pay attention: The Converter is still not written, follow next steps -->
      <Button x:Name="DeleteButton" 
              Content="Delete"
              HorizontalAlignment="Left" Height="64" Margin="74,579,0,-9" 
              VerticalAlignment="Top" Width="314" FontSize="24" 
              Visibility="{Binding IsVisible, 
                           Converter={StaticResource BooleanToVisibilityConverter}}" />
      

      转换器:

      public class BooleanToVisibilityConverter : IValueConverter
      {
          /// <summary>
          /// Converts a value.
          /// </summary>
          /// <param name="value">The value produced by the binding source.</param>
          /// <param name="targetType">The type of the binding target property.</param>
          /// <param name="parameter">The converter parameter to use.</param>
          /// <param name="culture">The culture to use in the converter.</param>
          /// <returns>A converted value. Returns Visible if the value is true; otherwise, collapsed.</returns>
          public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
          {
              return (bool)value ? Visibility.Visible : Visibility.Collapsed;
          }
      
          public object ConvertBack(object value, Type targetType, object parameter,CultureInfo culture)
          {
              throw new NotImplementedException();
          }
      }
      

      在 xaml 中的资源中,您应该添加转换器,以便您可以使用 StaticResource 访问它:

      <Application
      x:Class="UI.App"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:converters="using:UI.Converters">
      
      <converters:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
      

      然后根据需要更改 IsVisible 属性,如果为 true,它将绑定到 Visible,如果为 false,它将被折叠。

      if (condition)
      {
          IsVisible = true;
      }
      

      如需了解更多信息,您应该了解:bindingconverters

    【讨论】:

    • 你不能只给 IsVisible 属性一个 Visibility 数据类型而不是 bool 数据类型吗?
    【解决方案2】:

    您也可以使用XAML 和绑定:

    XAML:

    <UserControl.Resources>
       <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
    </UserControl.Resources>
    

    然后控制做某事:

    Visibility="{Binding IsVisible, Converter={StaticResource BooleanToVisibilityConverter}}"
    

    IsVisibleViewModel 中的bool 属性。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-15
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-26
    相关资源
    最近更新 更多