你可以将一个XAML组件的可见性与模型属性绑定。

huangapple go评论54阅读模式
英文:

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

&lt;TextBlock Text=&quot;{x:Bind ViewModel.Time}&quot; /&gt;

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(&quot;HH:mm:ss&quot;);

    [ObservableProperty]
    private Visibility timeVisibility = Visibility.Collapsed;
}

Note:

I'm using the CommunityToolkit.Mvvm NuGet package to implement the ViewModel.

&lt;TextBlock
    Text=&quot;{x:Bind ViewModel.Time}&quot;
    Visibility=&quot;{x:Bind ViewModel.TimeVisibility, Mode=OneWay}&quot; /&gt;

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 &amp;&amp; boolValue is true
            ? Visibility.Visible
            : Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}
&lt;TextBlock
    Text=&quot;{x:Bind ViewModel.Time}&quot;
    Visibility=&quot;{x:Bind ViewModel.IsTimeVisible, Mode=OneWay, Converter={StaticResource TrueToVisibleConverter}}&quot; /&gt;

huangapple
  • 本文由 发表于 2023年5月10日 17:28:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76216845.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定