2013-10-25 47 views
3

我有一個按鈕,名爲刪除。 我只是想讓只有當某些條件得到滿足, 所以我怎麼能做到這一點?如何在windows phone中以編程方式製作按鈕?

的按鈕創建的XAML代碼是

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

回答

8

你有一個可見性屬性。

你有一些方法可以做到這一點:

  1. 就在後面的代碼,你應該:

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

    上面的代碼應該幫助你使按鈕分別無形和有形。

    注意:這是不太可取的,它不是動態的,可能會導致重複和不必要的代碼。

  2. 更好的方式和更具活力的是:

    你可以做一個布爾財產和能見度按鈕綁定到它,就像這樣:

    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的資源,你應該添加的轉換器,因此您可以用靜態資源訪問:

    <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屬性爲您的需要,如果屬實將勢必可見,如果假的,它會崩潰。

    if (condition) 
    { 
        IsVisible = true; 
    } 
    

    對於你應該瞭解更多信息:bindingconverters

+0

你不能只是gi將IsVisible屬性設置爲可見性數據類型而不是bool數據類型? –

1

你可以用XAML和結合做也:

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

然後在控制使某事物喜歡:

在XAML

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

和可見性是一個bool在ViewModel屬性

+0

+1但作爲一個說明......這隻適用於.NET 3.0及更高版本。 http://msdn.microsoft.com/en-us/library/system.windows.controls.booleantovisibilityconverter%28v=vs.110%29.aspx –

相關問題