【问题标题】:TextBlock as CheckBox content not displayingTextBlock 作为 CheckBox 内容不显示
【发布时间】:2015-12-14 14:31:29
【问题描述】:

我对 WPF 中的复选框有疑问。我正在尝试将 CheckBox 的内容明确设置为 TextBlock,这样我就可以设置 TextBlock 的前景而不影响框中的检查。

我正在对 StackPanel 中的 CheckBox 执行此操作,其中包含数据网格模板列中的复选框,如下所示:

<DataGridTemplateColumn Header="Effectivity" CellStyle="{StaticResource WhiteForeground}">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Expander x:Name="EffectivityExpander" Header="{Binding EffectivityString}" Style="{StaticResource DisableExpander}" Foreground="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGridCell}}, Path=Foreground}">
                <StackPanel>
                    <CheckBox>
                        <TextBlock Text="Next order" Foreground="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGridCell}}, Path=Foreground}" />
                    </CheckBox>
                    <CheckBox Content="Parts on order" />
                </StackPanel>
            </Expander>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

问题是示例 XAML 中的第一个复选框在运行时不显示任何文本,而第二个复选框则显示。在两个复选框上使用 VS 2015 的 Live Property Explorer 时,第一个在 Computed Values/Templated Parent/Content 下显示它的 Text 属性,第二个(显示的)将其文本显示为本地值。

我尝试删除绑定和样式以查看它们是否有任何效果,但仍然没有显示。

有人知道为什么第一个 TextBlock 没有显示吗?

编辑: 回答大家的问题,不只是前景色。使用 VS2015 的“Live Visual Tree”选择器,没有文本可供选择。

查看此屏幕截图(或截图):

在可视化树中选择第一个文本框时,应用程序中不会显示任何边框。此外,将鼠标悬停在文本所在的位置不允许我选中该框;点击进入扩展器。在正在运行的应用程序中移动鼠标似乎会在复选框本身旁边显示一个 1 像素宽的区域,该区域确实允许与框交互,位于 TextBlock 的开头。就好像 TextBlock 没有文本一样。

【问题讨论】:

  • Live Property Explorer 说第一个 TextBlock 的 Foreground 属性的值是什么?它可能与单元格的背景颜色相同吗?
  • 尝试从 TextBlock 中移除 Foreground 属性,并查看运行时是否出现文本。不要相信“Live Visual Tree”——我见过它失败的案例。这里没有加起来 - 使用 TextBlock 作为 CheckBox 的内容对我来说很好。
  • 我已经尝试从链上一直到数据网格本身删除样式甚至绑定,文本块没有显示。使用 Snoop 而不是 VS2015 进行检查表明该文本块具有默认值(因此没有文本)。也许这与 WPF 的 Modern UI 有关?

标签: c# wpf xaml


【解决方案1】:

您可能正在使用白色前景,这可能会使文本显示为白色,因此无法看到。

代码如下:

<Window x:Class="WpfDataGrid._32635114.Win32635114"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Win32635114" Height="300" Width="300">

    <Window.Resources>
        <Style TargetType="DataGridCell" x:Key="WhiteForeground">
            <Setter Property="Foreground" Value="Red"/>
        </Style>

        <Style TargetType="Expander" x:Key="DisableExpander">
            <Setter Property="Background" Value="BurlyWood"/>
        </Style>

    </Window.Resources>

    <StackPanel>
        <DataGrid x:Name="MyGrid" Height="270" AutoGenerateColumns="False" ItemsSource="{Binding Values}">
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="Effectivity" CellStyle="{StaticResource WhiteForeground}">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Expander x:Name="EffectivityExpander" Header="{Binding EffectivityString}" Style="{StaticResource DisableExpander}" Foreground="{Binding Foreground, RelativeSource={RelativeSource AncestorType={x:Type DataGridCell}}}">
                                <StackPanel>
                                    <CheckBox>
                                        <TextBlock Text="Next order" Foreground="{Binding Foreground, RelativeSource={RelativeSource AncestorType={x:Type DataGridCell}}}" />
                                    </CheckBox>
                                    <CheckBox Content="Parts on order" />
                                </StackPanel>
                            </Expander>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

    </StackPanel>
</Window>

MainWindow.cs

namespace WpfDataGrid._32635114
{
    /// <summary>
    /// Interaction logic for Win32635114.xaml
    /// </summary>
    public partial class Win32635114 : Window
    {
        public Win32635114()
        {
            InitializeComponent();

            DataStore store = new DataStore();
            this.DataContext = store;
        }
    }
}

DataStore.cs

    namespace WpfDataGrid._32635114
{
    public class DataStore
    {
        private List<Record> _values;
        public List<Record> Values { get { return _values; } }

        public DataStore() {
            _values = new List<Record>();
            _values.Add(new Record() { EffectivityString = "somestring1" });
            _values.Add(new Record() { EffectivityString = "somestring2" });
            _values.Add(new Record() { EffectivityString = "somestring3" });
            _values.Add(new Record() { EffectivityString = "somestring4" });
        }
    }

    public class Record
    {
        public String EffectivityString { get; set; }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-05
    相关资源
    最近更新 更多