none
TextBlock foreground binding not work. RRS feed

  • Question

  • i'm using .net framework 3.5 sp1,xaml code:
     <Border Padding="5,5,5,5"  Margin="0,1,0,0" CornerRadius="8,8,8,8" Width="Auto">
            <StackPanel HorizontalAlignment="Stretch">
          <Expander x:Name="CompositeExpander" IsExpanded="{Binding IsExpandedView}"  Header="{Binding Path=EventAlarmCommandsView}" Content="{Binding Path=EventAlarmDetailsView}">
                    <Expander.HeaderTemplate>
                        <DataTemplate>
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="Auto" />
                                </Grid.ColumnDefinitions>
                                <TextBlock Grid.Column="0" x:Name="EventHeaderTextBlock"  Margin="0,5,0,0" Text="{Binding Path=DataContext.TextForegroundColor, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Expander}}}" Foreground ="{Binding Path=DataContext.TextForegroundColor, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TextBlock}}}" /> <!--Text="{Binding Path=DataContext.HeaderInfo, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Expander}}}"/>-->                                   
                                <ContentPresenter Grid.Column="1" HorizontalAlignment="Right"  Content="{Binding}" />
                            </Grid>
                        </DataTemplate>
                    </Expander.HeaderTemplate>
                </Expander>          
            </StackPanel>
        </Border>
    ViewMode Code:
     public static readonly DependencyProperty HeaderInfoProperty =
               DependencyProperty.Register("HeaderInfo", typeof(string), typeof(EventAlarmCompositeViewModel), null);
    
            public static readonly DependencyProperty TextForegroundColorProperty =
               DependencyProperty.Register("TextForegroundColor", typeof(Color), typeof(EventAlarmCompositeViewModel), null);
    
    ....
    
                BindingOperations.SetBinding(this, TextForegroundColorProperty, new Binding("TextForegroundColor") { Source = item });
    TextBlock.Foreground binding not work.but TextBlock.Text work.

    i try to change binding mode. but it not work also. xaml code:
    <Border Padding="5,5,5,5"  Margin="0,1,0,0" CornerRadius="8,8,8,8" Width="Auto">
            <StackPanel HorizontalAlignment="Stretch">
          <Expander x:Name="CompositeExpander" IsExpanded="{Binding IsExpandedView}"  Header="{Binding Path=EventAlarmCommandsView}" Content="{Binding Path=EventAlarmDetailsView}">
                    <Expander.HeaderTemplate>
                        <DataTemplate>
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="Auto" />
                                </Grid.ColumnDefinitions>
                                <TextBlock Grid.Column="0" x:Name="EventHeaderTextBlock"  Margin="0,5,0,0" Text="{Binding Path=DataContext.TextForegroundColor, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Expander}}}" Foreground ="{Binding Path=TextForegroundColorInfo}" /> <!--Text="{Binding Path=DataContext.HeaderInfo, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Expander}}}"/>-->                                   
                                <ContentPresenter Grid.Column="1" HorizontalAlignment="Right"  Content="{Binding}" />
                            </Grid>
                        </DataTemplate>
                    </Expander.HeaderTemplate>
                </Expander>          
            </StackPanel>
        </Border>
    ViewMode Code:
     public Color TextForegroundColorInfo
            {
                get { return Colors.Red; }
            }
    It is a XAML bug? How can i solve this qustion.



    Tuesday, November 10, 2009 4:53 AM

Answers

  • Hi,

    What exactly are you trying to achieve.

    You can bind your foreground to a solidcolorbrush

    <

     

    SolidColorBrush x:Key="StyleTextBrush" Color="Red"/>

     

     

    <TextBlock Foreground="{StaticResource StyleTextBrush}" />

     

     

    if you want to do it in a property you can return a SolidColorBrush.

    Hope that helps.


    vb.net GUI
    • Proposed as answer by giovhan Tuesday, November 10, 2009 5:45 AM
    • Marked as answer by floating Tuesday, November 10, 2009 5:50 AM
    Tuesday, November 10, 2009 5:45 AM

All replies

  • Hi,

    What exactly are you trying to achieve.

    You can bind your foreground to a solidcolorbrush

    <

     

    SolidColorBrush x:Key="StyleTextBrush" Color="Red"/>

     

     

    <TextBlock Foreground="{StaticResource StyleTextBrush}" />

     

     

    if you want to do it in a property you can return a SolidColorBrush.

    Hope that helps.


    vb.net GUI
    • Proposed as answer by giovhan Tuesday, November 10, 2009 5:45 AM
    • Marked as answer by floating Tuesday, November 10, 2009 5:50 AM
    Tuesday, November 10, 2009 5:45 AM
  • aha.......Tks...
    Tuesday, November 10, 2009 5:49 AM