Лучший способа и более динамичный является:
Вы можете сделать свойство Его и привязываете кнопку видимости к нему, как это:
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 свойство для вашей потребности, если это верно, то будет связано Видимый, если false, он будет свернут.
if (condition)
{
IsVisible = true;
}
Для получения дополнительной информации вы должны узнать: binding, converters.
Не можете ли вы просто gi v Свойство IsVisible: тип данных видимости вместо типа данных bool? –