TextBlock作为CheckBox内容未显示

时间:2015-09-17 16:09:20

标签: c# wpf xaml

WPF中的Check Box存在问题。我正在尝试将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的“实时可视树”选择器,没有可供选择的文本。

请参阅此屏幕截图(或剪辑): Selection of second item

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

1 个答案:

答案 0 :(得分:1)

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

Screen shot when run

以下是代码:

<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; }
    }
}
相关问题