简体   繁体   中英

WPF databinding not working for border or textblock in styled tabitem

I am trying to bind a border's background and text in a textblock to change on events. Both of these items exist inside a tab header which has been styled for the application.

If I hard code the color and text everything works properly, but when I try to bind the background color and text they no longer appear. What am I missing to get the databinding working properly?

xaml

<UserControl x:Class="Project.TabPanel"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:controls="clr-namespace:Project"
             DataContext="{Binding RelativeSource={RelativeSource Self}}"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="600">

    <TabControl Style="{StaticResource LeftTabControl}" Background="#FAFAFAFA" HorizontalAlignment="Stretch">
        <TabItem x:Name="ConnectionLabelTab" Style="{StaticResource Tab2}" Focusable="False">
            <TabItem.HeaderTemplate>
                <DataTemplate>
                    <Border x:Name="ConnectionLabelBorder" Background="{Binding LabelColor}" Width="70">
                        <TextBlock x:Name="ConnectionLabelText"
                            Text="{Binding LabelText}" Padding="0,4,0,4"
                            Foreground="Black" HorizontalAlignment="Center" VerticalAlignment="Center" 
                            FontSize="10"/>
                    </Border>
                </DataTemplate>
            </TabItem.HeaderTemplate>
        </TabItem>
    </TabConrol>
</UserControl>

xaml.cs

/// <summary>
/// Interaction logic for TabPanel.xaml
/// </summary>
public partial class TabPanel : UserControl
{
    String green = "#FF0A7E07";
    BrushConverter bc = new BrushConverter();

    Brush labelcolor;
    String labeltext;

    public TabPanel()
    {
        labelcolor = (Brush)bc.ConvertFromString(green);
        labeltext = "Connected";

        InitializeComponent();   
    }

    public Brush LabelColor { get { return labelcolor; } }
    public String LabelText { get { return labeltext; } }
}

You have the wrong DataContext in you DataTemplate, simply add:

DataContext="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=DataContext}" 

to the BorderControl in your DataTemplate, complete Template:

<DataTemplate>
    <Border x:Name="ConnectionLabelBorder" DataContext="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=DataContext}" Background="{Binding LabelColor}" Width="70">
        <TextBlock x:Name="ConnectionLabelText"
                   Text="{Binding LabelText}" Padding="0,4,0,4"
                   Foreground="Black" HorizontalAlignment="Center" VerticalAlignment="Center"
                   FontSize="10" />
    </Border>
</DataTemplate>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM