[英]Bind textblock visibility with custom property of grid
Hello,你好,
After reading lots of topics about visibility binding for hours, I'm asking here because I don't manage to make my case works.在阅读了许多关于可见性绑定的主题数小时后,我在这里问是因为我无法使我的案例有效。
I have a grid with a custom attached property (type System.Windows.Visibily) which I want to use to display (or not) a textblock inside the grid (by binding).我有一个带有自定义附加属性(类型 System.Windows.Visibily)的网格,我想用它来显示(或不显示)网格内的文本块(通过绑定)。 Also I want to change the visibility everytime the custom attached property change.
我也想在每次自定义附加属性更改时更改可见性。
What I have done so far : CustomProperties class :到目前为止我所做的: CustomProperties 类:
public static class CustomProperties
{
public static readonly DependencyProperty starVisibilityProperty =
DependencyProperty.RegisterAttached("starVisibility",
typeof(System.Windows.Visibility), typeof(CustomProperties),
new FrameworkPropertyMetadata(null));
public static System.Windows.Visibility GetStarVisibility(UIElement element)
{
if (element == null)
throw new ArgumentNullException("element");
return (System.Windows.Visibility)element.GetValue(starVisibilityProperty);
}
public static void SetStarVisibility(UIElement element, System.Windows.Visibility value)
{
if (element == null)
throw new ArgumentNullException("element");
element.SetValue(starVisibilityProperty, value);
}
}
Then here is my xaml :然后这是我的 xaml :
<Grid Name="server1State" Grid.Row="1" local:CustomProperties.StarVisibility="Hidden">
<TextBlock Name="server1Star" Text="" FontFamily="{StaticResource fa-solid}" FontSize="30" Margin="10" Foreground="#375D81" Visibility="{Binding ElementName=server1State, Path=server1State.(local:CustomProperties.starVisibility)}"/>
</Grid>
But when I run my app, the textblock is absolutely not hidden, this is visible, and never change.但是当我运行我的应用程序时,文本块绝对没有隐藏,这是可见的,永远不会改变。 I have tried lots of things with Path and also INotifyPropertyChanged but as I am working with static custom attached property, I didn't manage to make it works.
我已经尝试了很多关于 Path 和 INotifyPropertyChanged 的事情,但是当我使用静态自定义附加属性时,我没有设法让它工作。
Maybe some of you could help me, thanks .也许你们中的一些人可以帮助我,谢谢。
Your Binding.Path
on the TextBlock
is wrong.您在
TextBlock
上的Binding.Path
是错误的。
Since I've read from your comment, that you prefer to use a boolean property, I'll show how to convert the bool
value to a Visibility
enumeration value using the library's BooleanToVisibilityConverter
.由于我从您的评论中了解到您更喜欢使用布尔属性,因此我将展示如何使用库的
BooleanToVisibilityConverter
将bool
值转换为Visibility
枚举值。 I think you may already got it, but then got confused due to your wrong Binding.Path
:我想你可能已经明白了,但由于你错误的
Binding.Path
感到困惑:
CustomProperties.cs自定义属性.cs
public class CustomProperties : DependencyObject
{
#region IsStarVisibile attached property
public static readonly DependencyProperty IsStarVisibileProperty = DependencyProperty.RegisterAttached(
"IsStarVisibile",
typeof(bool),
typeof(CustomProperties),
new PropertyMetadata(default(bool)));
public static void SetIsStarVisibile(DependencyObject attachingElement, bool value) => attachingElement.SetValue(CustomProperties.IsStarVisibileProperty, value);
public static bool GetIsStarVisibile(DependencyObject attachingElement) => (bool)attachingElement.GetValue(CustomProperties.IsStarVisibileProperty);
#endregion
}
MainWindow.xaml主窗口.xaml
<Window>
<Window.Resources>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
</Window.Resources>
<Grid Name="Server1StateGrid"
CustomProperties.IsStarVisibile="False">
<TextBlock Text=""
Visibility="{Binding ElementName=Server1StateGrid,
Path=(CustomProperties.IsStarVisibile),
Converter={StaticResource BooleanToVisibilityConverter}}" />
</Grid>
</Window>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.