英文:
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"/>
英文:
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"/>
答案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 => getVisible();
XAML:
<Button Visibility="{Binding IsVisible}" />
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
:
<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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论