Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5991933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:27:34+00:00 2026-05-22T23:27:34+00:00

I want to add a DataTrigger to my base TextBox style so that it

  • 0

I want to add a DataTrigger to my base TextBox style so that it sets the foreground color to a different value if it is inside of a DataGridCell that is selected. Here is what my trigger looks like:

<Style.Triggers>
    <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGridCell}}, Path=IsSelected}"
                 Value="True">
        <Setter Property="Foreground"
                Value="White" />
    </DataTrigger>
</Style.Triggers>

This works great, except that when my TextBox is not in a DataGrid the Binding fails and writes an exception to the output window. How can I prevent this.

I basically want to say if Parent is a DataGridCell then apply this trigger otherwise ignore it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T23:27:35+00:00Added an answer on May 22, 2026 at 11:27 pm

    In general just only apply the style where applicable. If you want implicit application use nested styles:

    <Style TargetType="{x:Type DataGrid}">
        <Style.Resources>
            <Style TargetType="{x:Type TextBox}">
                <Style.Triggers>
                    <DataTrigger
                            Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGridCell}}, Path=IsSelected}"
                            Value="True">
                        <Setter Property="Foreground" Value="White" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Style.Resources>
    </Style>
    

    If you have other parts which you want to apply to all TextBoxes take out those parts in a serarate style and use BasedOn in the style which applies to the TextBoxes inside the DataGrid.


    Edit: MultiDataTrigger seems to return right away if a condition is not met so you can avoid binding errors:

    <Style TargetType="{x:Type TextBox}">
        <Style.Resources>
            <vc:HasAncestorOfTypeConverter x:Key="HasAncestorOfTypeConverter" AncestorType="{x:Type DataGridCell}" />
        </Style.Resources>
        <Style.Triggers>
            <MultiDataTrigger>
                <MultiDataTrigger.Conditions>
                    <Condition
                            Binding="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource HasAncestorOfTypeConverter}}"
                            Value="True" />
                    <Condition
                            Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGridCell}}, Path=IsSelected}"
                            Value="True" />
                </MultiDataTrigger.Conditions>
                <Setter Property="Foreground" Value="Red" />
            </MultiDataTrigger>
        </Style.Triggers>
    </Style>
    
    public class HasAncestorOfTypeConverter : IValueConverter
    {
        public Type AncestorType { get; set; }
    
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null) return false;
            DependencyObject current = value as DependencyObject;
            while (true)
            {
                current = VisualTreeHelper.GetParent(current);
                if (current == null)
                {
                    return false;
                }
                if (current.GetType() == AncestorType)
                {
                    return true;
                }
            }
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
    

    This of course causes quite som overhead so it might not be such a good solution, then again if the RelativeSource-binding fails it also had to go up the tree first.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of string values that I want add to a hashtable
I have an ObservableCollection<Object> that contains two different types. I want to bind this
I want add to my Windows phone app masked textbox, are there ready-made control
I have a report that I want add a line beneath the even rows
I want add a separator in Blackberry Listfield inside drawListRow with graphics.drawLine Method but
I have a table with some rows and inputs inside them. I want add
I want add new fonts for Android. I found a tutorial that asks me
I am working on magento. I want add a functionality that when user places
I want to add items in a LaTeX-document. Say for example, that I want
REVISION I want add a onclick to a element that handles a Ajax request,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.