【问题标题】:How to hide/disable one TextBlock control from WPF usercontrol?如何从 WPF 用户控件中隐藏/禁用一个 TextBlock 控件?
【发布时间】:2016-08-05 04:52:59
【问题描述】:

我有一个带标题的切换按钮,我创建了 ToggleSwitch.xaml 用户控件,以便我可以在多个页面中使用它们。每件事都进行得很完美。

但是在一页中,我需要在 DataGrid 中显示切换开关按钮,以便用户可以更改状态,但是在使用 ToggleSwitch UserControl 时,TextBox 也会占用一些空间,使我的设计看起来很糟糕。

我只想在 DataGrid 上显示 ToggleSwitch 按钮,而不是带有一些文本的 TextBox。

我想隐藏 TextBox,以免影响我的设计。

谢谢你,如果有人能帮忙,你可以看到下面的代码sn-p。

 <StackPanel Orientation="Horizontal" x:Name="LayoutRoot" Margin="0,0,-23,0">
    <ToggleButton Name="toggleButton"  VerticalAlignment="Center" Click="ToggleButton_OnClick" IsChecked="{Binding Path=StateChecked}" Cursor="Hand" Style="{DynamicResource AnimatedSwitch}" Height="13" Width="23" Margin="0,0,0,0" />
    <TextBlock Name="tbText" Text="{Binding Path=ControlText}" VerticalAlignment="Center" Width="279" Margin="15,8,0,7"></TextBlock>
</StackPanel>

【问题讨论】:

  • 也许你可以在你的用户控件中添加一个布尔值,当它被标记时它会隐藏你的文本框
  • 您能否详细说明@DarkTemplar?
  • 我需要看你的用户控件的xaml,我会写一个例子。
  • 可见性 = 已折叠
  • 可见性 = 已折叠,折叠整个用户控件,我只想隐藏用户控件中的文本框 @AnjumSKhan

标签: wpf wpf-controls wpfdatagrid


【解决方案1】:

您可以使用Dependency Properties 实现此目的。将文本框的可见性绑定到用户控件中的依赖属性。

首先像这样创建依赖属性:

public Visibility TextBlockVisibilityProperty
{
    get { return (Visibility)GetValue(TextBlockVisibilityPropertyProperty); }
    set { SetValue(TextBlockVisibilityPropertyProperty, value); }
}

// Using a DependencyProperty as the backing store for TextBlockVisibilityProperty.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty TextBlockVisibilityPropertyProperty =
    DependencyProperty.Register("TextBlockVisibilityProperty", typeof(Visibility), typeof(MaintenancePage), new PropertyMetadata(0));

然后在 xaml 中将属性绑定到textblock 可见性,如下所示

<StackPanel Orientation="Horizontal" x:Name="LayoutRoot" Margin="0,0,-23,0">
    <ToggleButton Name="toggleButton"  VerticalAlignment="Center" Click="ToggleButton_OnClick" IsChecked="{Binding Path=StateChecked}" Cursor="Hand" Style="{DynamicResource AnimatedSwitch}" Height="13" Width="23" Margin="0,0,0,0" />
    <TextBlock Name="tbText" Text="{Binding Path=ControlText}" VerticalAlignment="Center" Width="279" Margin="15,8,0,7" Visibility="{Binding TextBlockVisibilityProperty}"></TextBlock>
</StackPanel>

现在,当在 xaml 中重用控件时,设置TextBlockVisibilityProperty,同时根据需要定义控件。这将隐藏textblock

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-13
    • 2021-02-25
    • 1970-01-01
    • 2023-03-02
    • 2011-10-05
    • 2012-08-23
    相关资源
    最近更新 更多