C# 将textblock可见性与栅格的自定义属性绑定

C# 将textblock可见性与栅格的自定义属性绑定,c#,wpf,xaml,binding,attached-properties,C#,Wpf,Xaml,Binding,Attached Properties,你好, 在读了很多关于可见性绑定的话题几个小时后,我在这里问这个问题,因为我无法使我的案例有效 我有一个带有自定义附加属性类型System.Windows.Visibily的网格,我想使用它通过绑定在网格内显示或不显示文本块。此外,每次自定义附着属性更改时,我都要更改可见性 到目前为止我所做的: 自定义属性类: 这是我的xaml: 但当我运行我的应用程序时,文本块绝对不会隐藏,它是可见的,并且永远不会改变。我已经尝试了很多关于Path和INotifyPropertyChanged的东西,但是由于

你好,

在读了很多关于可见性绑定的话题几个小时后,我在这里问这个问题,因为我无法使我的案例有效

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

到目前为止我所做的: 自定义属性类:

这是我的xaml:

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

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

您的绑定。文本块上的路径错误

由于我从您的评论中了解到,您更喜欢使用布尔属性,因此我将演示如何使用库的属性将布尔值转换为可见性枚举值。 我想您可能已经得到了它,但由于绑定错误而感到困惑。路径:

CustomProperties.cs

MainWindow.xaml


使用自定义属性而不是转换器有什么原因吗?另外,为什么属性在网格上而不是文本框上?我的自定义属性首先是一个布尔值,我想在绑定期间用BooleantVisibilityConverter将其转换为可见性,但由于我没有找到如何使其工作,我尝试删除转换步骤。但我当然更喜欢布尔值。我希望在网格上有一个属性,因为稍后我会在网格中有很多东西,它们会根据属性值而改变。它将成为一个状态属性,将由转换器转换。我假设您使用的是MVVM,如果是,为什么不公开各种可见性状态的属性集合,例如,假设您有一个截止日期、发票状态和已接受状态—IsEdit、IsPassedDeadline和IsAccepted。然后在VM中执行业务逻辑,而不是自定义属性。您好,谢谢您的回答!!我仍然有一个错误,但我认为我们几乎成功了,因为我从未遇到过这样的错误:当我想运行应用程序时,它失败了,因为“System.Windows.Baml2006.TypeConverterMarkupExtension”上的XamlParseException=>Provide value抛出了一个异常。“行号”88“行位置”149。它发生在我添加路径时,而ConverterIt在我添加路径时失败,而且我必须编写local:CustomProperties.IsStarVisibleYes,该示例应该是泛型的。名称空间是特定的。所以我离开了名称空间。当然,您必须限定XAML中使用的每个类型,以便XAML解析器能够解析这些类型。关于此错误,您是否已将BooleanToVisibilityConverter添加到范围内的任何ResourceDictionary中?这个例子确实有效。是的,在窗口内。资源和您一样。下面的编译很好{Binding ElementName=server1State,Converter={StaticResource BooleantVisibilityConverter},它只在我尝试添加路径时失败。CustomProperties.IsStarVisibile的附加属性实现与我的完全一样吗?
    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);
        }
    }
            <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>
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
}
<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>