TabItem标头丢失绑定到Property =" TextElement.Foreground"

时间:2018-03-13 11:48:02

标签: wpf xaml

ControlTemplate

的以下TabItem
<Style x:Key="MyTabItem" TargetType="{x:Type TabItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TabItem}">
                    <Grid>
                        <Border x:Name="Border">
                            <ContentPresenter VerticalAlignment="Center" Margin="5" HorizontalAlignment="Center" ContentSource="Header">
                            </ContentPresenter>
                        </Border>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter TargetName="Border" Property="Background" Value="Red" />
                            <Setter TargetName="Border" Property="TextElement.FontWeight" Value="Bold"/>
                            <Setter TargetName="Border" Property="TextElement.Foreground" Value="Yellow" />
                        </Trigger>
                        <Trigger Property="IsSelected" Value="False">
                            <Setter TargetName="Border" Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
                            <Setter TargetName="Border" Property="TextElement.FontWeight" Value="Normal"/>
                            <Setter TargetName="Border" Property="TextElement.Foreground" Value="Black" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>\

工作o.k.当我使用它时

<TabItem Header="Details">

但在使用时不再存在:

<TabItem.Header>
        <TextBlock >
            <Run Text="Details " />
            <Run Text="{Binding ViewModel.ToFrom, Mode=OneWay}" />
        </TextBlock>
 </TabItem.Header>

<TabItem.Header>
            <TextBlock Content="Test"/>
</TabItem.Header>

在一个简单的测试项目中也尝试了相同的结果 我只能补充一点 <TabItem.Header>Test</TabItem.Header>
也有效。

这令我失望。我试图以某种方式将TextBlock绑定到附加属性TextElement,但没有运气。

有人知道如何解决这个问题吗? 为什么它不能像我预期的那样工作?

2 个答案:

答案 0 :(得分:0)

尝试这样的事情;

您的主窗口.xaml文件

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    x:Class="WpfApplication1.MainWindow"
    mc:Ignorable="d"
    Title="MainWindow"
    Height="350"
    Width="525">

    <Grid>
    <TabControl>
        <TabItem Style="{StaticResource MyTabItem}" >
            <TabItem.Header>
                <TextBlock>
        <Run Text="Details " />
                    <Run  Text="{Binding ToFrom, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,NotifyOnSourceUpdated=True}"/>
        </TextBlock>
            </TabItem.Header>
        </TabItem>
    </TabControl>
</Grid>

您的MainWindow.xaml.cs文件如下:

 public partial class MainWindow : Window
 {
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new TestViewModel();
    }
 }

你的viewmodel是这样的:

public class TestViewModel
{
    public string ToFrom { get; set; }

    public TestViewModel()
    {
        ToFrom = "test";
    }
}

这应该可以正常工作..

答案 1 :(得分:0)

最可能的解释可能是您有 int main() { int maxR; int maxC; int generations; int i=0; int j=0; int k=0; int n; //neighbour count char state; char **board; //original boardfor comparison char **newBoard; //boardto make changes to scanf("%d %d %d",&maxR,&maxC,&generations); //take input board= (char**)malloc(maxR * sizeof(char*)); //allocating memory newBoard=(char**) malloc(maxR * sizeof(char*)); //allocating memory for(i=0; i<maxR; i++) { board[i] = malloc(maxC * sizeof (char)); //allocating memory newBoard[i] = malloc(maxC * sizeof (char)); //allocating memory for(j=0; j<maxC; j++) { scanf (" %c", &board[i][j]); //getting input } } for(i=0; i<=generations; i++ ) { for (j=0; j<maxR; j++) { for (k=0; k<maxC; k++) { state=board[j][k]; n=countNeighbours(board,maxR,maxC,j,k); if(state == '1') { //if the cell is alive if(n==2 || n==3) newBoard[j][k] = '1'; //if the cell has 2 or 3 neighbours then it lives else newBoard[j][k]='0'; //else the cell dies } else { //else (if) the cell is dead if(n==3) newBoard[j][k]='1'; //but has 3 neibours then the cell become alive else newBoard[i][j]='0'; //else it dies } } } memcpy(board, newBoard,sizeof(board)); //copy the updated grid to the old one } printBoard(board,maxR,maxC); deallocate(board,maxR); //deallocatethe memory deallocate(copyGrid,maxR); //deallocatethe memory 的默认样式,它会覆盖TextBlock属性。显式使用空样式时,可以防止使用默认样式。

这样的事情消除了默认样式作为错误的来源,至少为了调试目的:

Foreground