英文:
How can I bind the visibility of a XAML Component to a model property?
问题
我想将以下部分的可见性绑定到一个变量。
<TextBlock Text="{x:Bind ViewModel.Time}" />
我尝试将其绑定到可见性枚举,并在谷歌上搜索了该问题。对于 WinUI 3,没有找到解决方案。
英文:
I want to bind the visibility of
<TextBlock Text="{x:Bind ViewModel.Time}" />
to a variable.
I tried to bind it to Visibility enum and google the problem. Found no solution for winui 3.
答案1
得分: 0
As @Simon Mourier mentioned in a comment, you can just bind to a "Visibility" property in your ViewModel:
public partial class ViewModel : ObservableObject
{
[ObservableProperty]
private string time = DateTime.Now.ToString("HH:mm:ss");
[ObservableProperty]
private Visibility timeVisibility = Visibility.Collapsed;
}
Note:
I'm using the CommunityToolkit.Mvvm NuGet package to implement the ViewModel.
<TextBlock
Text="{x:Bind ViewModel.Time}"
Visibility="{x:Bind ViewModel.TimeVisibility, Mode=OneWay}" />
Or, if you want to avoid "UI" related code in your ViewModel, you can bind the TextBlock
's Visibility
to a bool
and use a ValueConverter
like this:
public partial class ViewModel : ObservableObject
{
[ObservableProperty]
private string time = DateTime.Now.ToString();
[ObservableProperty]
private bool isTimeVisible = false;
}
public class TrueToVisibleConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
return value is bool boolValue && boolValue is true
? Visibility.Visible
: Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
<TextBlock
Text="{x:Bind ViewModel.Time}"
Visibility="{x:Bind ViewModel.IsTimeVisible, Mode=OneWay, Converter={StaticResource TrueToVisibleConverter}}" />
英文:
As @Simon Mourier mentioned in a comment, you can just bind to a "Visibility" property in your ViewModel:
public partial class ViewModel : ObservableObject
{
[ObservableProperty]
private string time = DateTime.Now.ToString("HH:mm:ss");
[ObservableProperty]
private Visibility timeVisibility = Visibility.Collapsed;
}
Note:
I'm using the CommunityToolkit.Mvvm NuGet package to implement the ViewModel.
<TextBlock
Text="{x:Bind ViewModel.Time}"
Visibility="{x:Bind ViewModel.TimeVisibility, Mode=OneWay}" />
Or, if you want to avoid "UI" related code in your ViewModel, you can bind the TextBlock
's Visibility
to a bool
and use a ValueConverter
like this:
public partial class ViewModel : ObservableObject
{
[ObservableProperty]
private string time = DateTime.Now.ToString();
[ObservableProperty]
private bool isTimeVisible = false;
}
public class TrueToVisibleConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
return value is bool boolValue && boolValue is true
? Visibility.Visible
: Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
<TextBlock
Text="{x:Bind ViewModel.Time}"
Visibility="{x:Bind ViewModel.IsTimeVisible, Mode=OneWay, Converter={StaticResource TrueToVisibleConverter}}" />
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论