WPF 绑定可见性从函数

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

WPF Bind Visible From function

问题

在我的主要部分,我有3个角色。

我想在角色1中显示一些按钮。
我想在角色1和2中显示一些按钮。
等等。

我尝试使用Visibilityby函数来设置可见性,但它不起作用。

<Button Tag="2,3" Visibility="{ Binding getVisible()}" Content="צפייה בבקשות" Click="AddHost_Click" Background="{x:Null}" BorderBrush="{x:Null}" Foreground="#FFA2A2A2" FontSize="15" FontWeight="Bold" VerticalAlignment="Center"/>

动态地如何更改可见性
WPF 绑定可见性从函数

英文:

In my main I have 3 roles

Some buttons I want to show in role 1
Some buttons I want to show in role 1 and 2
etc.

I try to set the Visibilityby function but it does not work

<Button Tag="2,3" Visibility="{ Binding getVisible()}" Content="צפייה בבקשות" Click="AddHost_Click" Background="{x:Null}" BorderBrush="{x:Null}" Foreground="#FFA2A2A2" FontSize="15" FontWeight="Bold" VerticalAlignment="Center"/>

How Dynamically I can change the Visible
WPF 绑定可见性从函数

答案1

得分: 1

不能绑定到像getVisible()这样的方法。你只能绑定到公共属性,所以你需要创建一个属性。它可以调用getVisible()

public Visibility IsVisible => getVisible();

XAML:

<Button Visibility="{Binding IsVisible}" />

如果IsVisible/getVisible()返回一个bool值,你还需要将其转换为Visibility值。你可以使用内置的BooleanToVisibilityConverter来实现这一点:

<Button Tag="2,3" Content="צפייה בבקשות" Click="AddHost_Click" Background="{x:Null}" BorderBrush="{x:Null}" 
        Foreground="#FFA2A2A2" FontSize="15" FontWeight="Bold" VerticalAlignment="Center">
    <Button.Visibility>
        <Binding Path="IsVisible">
            <Binding.Converter>
                <BooleanToVisibilityConverter />
            </Binding.Converter>
        </Binding>
    </Button.Visibility>
</Button>
英文:

You cannot bind to methods like getVisible(). You can only bind to public properties so you need to create a property. It may call getVisible():

public Visibility IsVisible =&gt; getVisible();

XAML:

&lt;Button Visibility=&quot;{Binding IsVisible}&quot; /&gt;

If IsVisible/getVisible() returns a bool, you also need to convert this to a Visibility value. You can do this using the built-in BooleanToVisibilityConverter:

&lt;Button Tag=&quot;2,3&quot; Content=&quot;צפייה בבקשות&quot; Click=&quot;AddHost_Click&quot; Background=&quot;{x:Null}&quot; BorderBrush=&quot;{x:Null}&quot; 
        Foreground=&quot;#FFA2A2A2&quot; FontSize=&quot;15&quot; FontWeight=&quot;Bold&quot; VerticalAlignment=&quot;Center&quot;&gt;
    &lt;Button.Visibility&gt;
        &lt;Binding Path=&quot;IsVisible&quot;&gt;
            &lt;Binding.Converter&gt;
                &lt;BooleanToVisibilityConverter /&gt;
            &lt;/Binding.Converter&gt;
        &lt;/Binding&gt;
    &lt;/Button.Visibility&gt;
&lt;/Button&gt;

huangapple
  • 本文由 发表于 2020年1月3日 19:43:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/59578036.html
匿名

发表评论

匿名网友

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

确定