【问题标题】:Bind textblock visibility with custom property of grid将文本块可见性与网格的自定义属性绑定
【发布时间】:2020-04-02 12:34:46
【问题描述】:

您好,

在阅读了几个小时关于可见性绑定的大量主题后,我在这里问,因为我无法使我的案例有效。

我有一个带有自定义附加属性(类型 System.Windows.Visibily)的网格,我想用它来显示(或不显示)网格内的文本块(通过绑定)。我还想在每次自定义附加属性更改时更改可见性。

到目前为止我做了什么: 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);
        }
    }

那么这是我的 xaml :

            <Grid Name="server1State" Grid.Row="1" local:CustomProperties.StarVisibility="Hidden">
                <TextBlock Name="server1Star" Text="&#xf005;" FontFamily="{StaticResource fa-solid}" FontSize="30" Margin="10" Foreground="#375D81" Visibility="{Binding ElementName=server1State, Path=server1State.(local:CustomProperties.starVisibility)}"/>
            </Grid>

但是当我运行我的应用程序时,文本块绝对没有隐藏,这是可见的,并且永远不会改变。我已经用 Path 和 INotifyPropertyChanged 尝试了很多东西,但是当我使用静态自定义附加属性时,我没有设法让它工作。

也许你们中的一些人可以帮助我,谢谢

【问题讨论】:

  • 您是否有任何理由使用自定义属性而不是转换器?另外,为什么属性在 Grid 而不是文本框上?
  • 我的自定义属性首先是一个布尔值,我想在绑定期间使用 BooleanToVisibilityConverter 将其转换为可见性,但由于我没有找到如何使其工作,我试图删除“转换步骤” .但我当然更喜欢布尔值。而且我想在网格上有一个属性,因为稍后我将在网格内有很多可以根据属性值改变的东西。它将成为一个“状态属性”,将被转换器转换。
  • 我认为您正在使用 MVVM,如果是这样,您为什么不公开一组属性以用于各种可见性状态,例如假设您有截止日期、发票状态和已接受状态 - IsEditable、IsPassedDeadline 和 IsAccepted。然后在 VM 中而不是自定义属性中执行您的业务逻辑。

标签: c# wpf xaml binding attached-properties


【解决方案1】:

您在TextBlock 上的Binding.Path 错误。

由于我从您的评论中了解到您更喜欢使用布尔属性,因此我将展示如何使用库的 BooleanToVisibilityConverterbool 值转换为 Visibility 枚举值。 我想你可能已经明白了,但是由于你的错误Binding.Path而感到困惑:

CustomProperties.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

<Window>
  <Window.Resources>
    <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
  </Window.Resources>

  <Grid Name="Server1StateGrid"
        CustomProperties.IsStarVisibile="False">
    <TextBlock Text="&#xf005;" 
               Visibility="{Binding ElementName=Server1StateGrid,       
                            Path=(CustomProperties.IsStarVisibile), 
                            Converter={StaticResource BooleanToVisibilityConverter}}" />
  </Grid>
</Window>

【讨论】:

  • 您好,感谢您的回答!!我仍然有一个错误,但我认为我们几乎得到了它,因为我从未遇到过这个错误:当我想运行应用程序时,它失败并出现“XamlParseException”=>“在 'System.Windows.Baml2006.TypeConverterMarkupExtension' 上提供值”抛出一个例外。行号“88”和行位置“149”。它发生在我添加路径和转换器时
  • 添加路径时失败,而且我必须写“(本地:CustomProperties.IsStarVisible)”
  • 是的,这个例子应该是通用的。命名空间是特定的。所以我离开了命名空间。当然,您必须限定 XAML 中使用的 each 类型,以便 XAML 解析器可以解析这些类型。关于错误,您是否将BooleanToVisibilityConverter 添加到范围内的任何ResourceDictionary 中?这个例子肯定是有效的。
  • 是的,在 Window.Resources 中,就像你做的那样。以下编译良好“{Binding ElementName=server1State, Converter={StaticResource BooleanToVisibilityConverter}}”,仅在我尝试添加路径时失败。
  • 你的CustomProperties.IsStarVisibile的附加属性实现看起来和我的一模一样吗?
猜你喜欢
  • 2014-01-07
  • 2014-08-20
  • 2010-09-27
  • 2012-05-23
  • 1970-01-01
  • 2015-05-11
  • 2012-07-11
  • 2012-05-15
  • 1970-01-01
相关资源
最近更新 更多