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 522941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:24:22+00:00 2026-05-13T08:24:22+00:00

I have a DataTrigger that I recently refactored. It used to have the DataContext

  • 0

I have a DataTrigger that I recently refactored. It used to have the DataContext set to be a ListBoxItem. Now it is a ContentPresenter.

Here is the code:

<DataTemplate.Triggers>
    <DataTrigger Value="True">
        <DataTrigger.Binding>
            <MultiBinding Converter="{StaticResource DisableWorkItemConverter}">
                <Binding ElementName="MainForm" Path="PickedWorkItemID"/>
                <Binding Path="WorkItemForColumn.Id"/>
            </MultiBinding>
        </DataTrigger.Binding>
        <Setter Property="IsEnabled" Value="False"/>
        <Setter Property="loc:Main.IsCurrentItemEnabledChanged" Value="True"/>
    </DataTrigger>
</DataTemplate.Triggers>

In the OnChange of IsCurrentItemEnabledChanged I can see that I can get at the ListBoxItem with the following code (in the code behind):

listBoxItem = (ListBoxItem)Main.Instance.lstQueryResults.ItemContainerGenerator.
               ContainerFromItem(((ContentPresenter)d).Content);

However, there is no way that I can see to set the DataContext such that my Setter for IsEnabled will set the enabled status of the ListBoxItem rather than the ContentPresenter.

(I would do it in the OnChange of IsCurrentItemEnabledChanged, but that property is already a bit of a hack and it will not re-enable the item when the trigger is set to false.)

Any ideas?


It was suggested that I provide more to provide context:

Here is my XAML for the templates.

<DataTemplate x:Key="ColumnTemplate">
    <Border Name="ItemBorder" BorderBrush="Black" BorderThickness="1" CornerRadius="2" Padding="2">
        <WrapPanel>                   
            <TextBlock Margin="0,0,5,0">
                <TextBlock.Text>
                    <Binding Path="Name" Converter="{StaticResource GetVisibilityOfColumnTitles}"/>
                </TextBlock.Text>
            </TextBlock>
            <TextBlock Text="{Binding Value}" Margin="0,0,10,0" FontWeight="Bold" />
        </WrapPanel>
    </Border>
</DataTemplate>

<DataTemplate x:Key="RowTemplate">
    <Border x:Name="ItemBorder" BorderThickness="1" BorderBrush="#D4D4FF">
        <Grid x:Name="ItemGrid" Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsPresenter}}, Path=ActualWidth}" ScrollViewer.CanContentScroll="True" Margin="2">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="20" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="30" />
            </Grid.ColumnDefinitions>
            <Grid.Background>
                <Binding Path="WorkItemForColumn.Type" Converter="{StaticResource WorkItemTypeToColorConverter}" />
            </Grid.Background>

            <CheckBox VerticalAlignment="Center" Grid.Column="0" IsChecked="{Binding
                                      RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}},
                                      Mode=TwoWay, Path=IsSelected}" Name="chkIsSelected" />

            <ItemsControl Grid.Column="1" Margin="5,0,5,0" ItemsSource="{Binding}" ItemTemplate="{StaticResource ColumnTemplate}" >
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel Orientation="Horizontal"></WrapPanel>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            </ItemsControl>


            <Button HorizontalAlignment="Right" x:Name="btnPick" Grid.Column="3" Style="{StaticResource roundButton}" Width="15" Height="15" Tag="{Binding WorkItemForColumn.Id}" Margin="5,0,10,0">
                <Path Fill="DarkBlue">
                    <Path.Data>
                        <PathGeometry>
                            <PathFigure StartPoint="2,0" IsClosed="True">
                                <LineSegment Point="7,5"/>
                                <LineSegment Point="2,10"/>
                            </PathFigure>
                            <PathFigure StartPoint="2,2" IsClosed="True">
                                <LineSegment Point="5,5"/>
                                <LineSegment Point="2,8"/>
                            </PathFigure>
                        </PathGeometry>
                    </Path.Data>
                </Path>
            </Button>
        </Grid>
    </Border>
    <DataTemplate.Triggers>
        <DataTrigger Value="True">
            <DataTrigger.Binding>
                <MultiBinding Converter="{StaticResource DisableWorkItemConverter}">
                    <Binding ElementName="MainForm" Path="PickedWorkItemID"/>
                    <Binding Path="WorkItemForColumn.Id"/>
                </MultiBinding>
            </DataTrigger.Binding>
            <Setter Property="IsEnabled" Value="False"/>
            <Setter Property="loc:Main.IsCurrentItemEnabledChanged" Value="True"/>
        </DataTrigger>
    </DataTemplate.Triggers>
</DataTemplate>

Here is the XAML for the List Box:

<ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled" Button.Click="PickWorkItem_Click" SelectionMode="Multiple" ItemTemplate="{StaticResource RowTemplate}"  Name="lstQueryResults" SelectionChanged="lstQueryResults_SelectionChanged" >
    <ListBox.Resources>
        <Style TargetType="{x:Type ListBoxItem}">
            <Style.Resources>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black"/>
                <SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="Black"/>
            </Style.Resources>
        </Style>
    </ListBox.Resources>
</ListBox>

The DataContext is set in the Code like so:

private void ChangeQueryResultListSource(WorkItemCollection queryResults, bool b)
{
    // Un-hook the selection event while we change the ItemsSource
    Form.ToggleOnSelectEvent(false);

    // This sets the DataContext
    QueryDisplay = GetDisplayValues(queryResults);

    Form.QueryResultListSource = QueryDisplay;
    // Rewire the selection events back in
    Form.ToggleOnSelectEvent(true);


    foreach (WorkItem item in Pad.Keys)
    {
        Form.SelectQueryResultItem(item);
    }
}

private List<List<WorkItemColumn>> GetDisplayValues(WorkItemCollection queryResults)
{
    var result = new List<List<WorkItemColumn>>();
    foreach (WorkItem workItem in queryResults)
    {
        var row = GetQueryColumns(queryResults.DisplayFields, workItem);
        result.Add(row);
    }
    return result;
}

private List<WorkItemColumn> GetQueryColumns(DisplayFieldList fields, WorkItem workItem)
{
    var row = new List<WorkItemColumn>();
    foreach (FieldDefinition column in fields)
    {
        var workItemColumn = new WorkItemColumn { Name = column.Name, Value = workItem[column.Name], WorkItemForColumn = workItem };
        row.Add(workItemColumn);
    }

    return row;
}

WorkItemColumn is a class with a Name-Value pair and a reference to the data (a WorkItem object).

  • 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-13T08:24:22+00:00Added an answer on May 13, 2026 at 8:24 am

    Sorry for the short answer, but you should move the trigger and setter logic for IsEnabled back to the ListBoxItem style:

    <Style TargetType="{x:Type ListBoxItem}">
        <Style.Resources>
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black"/>
            <SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="Black"/>
        </Style.Resources>
        <Style.Triggers>
            <DataTrigger Value="True">
                <DataTrigger.Binding>
                    <MultiBinding Converter="{StaticResource DisableWorkItemConverter}">
                        <Binding ElementName="MainForm" Path="PickedWorkItemID"/>
                        <Binding Path="WorkItemForColumn.Id"/>
                    </MultiBinding>
                </DataTrigger.Binding>
                <Setter Property="IsEnabled" Value="False"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a usercontrol that I want to use as a ListBoxItem. <ListBox.ItemTemplate> <DataTemplate>
I want to have one Style DataTrigger inside my window.resources that can be used
I have a comboBox that has a datatrigger that set its SelectedIndex based on
Have a matrix report now that has Position, Hours and Wages for a location
I'm trying to set up a ContentTemplate that changes based on a DataTrigger. Syntatically,
I have a datatemplate containing an image that I want to be hidden if
I have a set of data that I'd like to present via a WPF
i have the following datatemplate in xaml and here i have textbox in datatemplate
I have several StackPanels that change visibility based on ToggleButtons. The code below works
I have a WPF data trigger that is set to fire when a value

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.