Recently, I needed to change the Thumb's Template. I want to change its shape to a Triangle like so. I used a Path as its ControlTemplate. Here's the code:
<Thumb
Width="100"
Height="30"
HorizontalAlignment="Left"
VerticalAlignment="Top">
<Thumb.Template>
<ControlTemplate>
<Path Fill="Red">
<Path.Data>
<PathGeometry>
<PathFigure>
<PolyLineSegment>
<PolyLineSegment.Points>
<PointCollection>
<Point X="0" Y="0" />
<Point X="{TemplateBinding Width}" Y="{TemplateBinding Height}" />
<Point X="0" Y="{TemplateBinding Height}" />
</PointCollection>
</PolyLineSegment.Points>
</PolyLineSegment>
</PathFigure>
</PathGeometry>
</Path.Data>
</Path>
</ControlTemplate>
</Thumb.Template>
</Thumb>
You can see that I tried to bind PolyLineSegment.Points to Thumb's Width and Height.
But Visual Studio throws an error:
"Error Object of type 'System.Windows.TemplateBindingExpression'
cannot be converted to type 'System.Double'."
I don't know why it can't work. Can anyone give me a hint?
Finally, I found where the problem is. I found it's because Point.X or Point.Y is not Dependency Property. So that's why binding doesn't work.
I resolve this question by binding to LineSegment.
<ControlTemplate>
<Path Fill="{TemplateBinding Background}">
<Path.Data>
<PathGeometry>
<PathFigure StartPoint="0 0">
<LineSegment>
<LineSegment.Point>
<MultiBinding Converter="{StaticResource thumb2PointConverter}" ConverterParameter="rightBottom">
<Binding Path="Width" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=Thumb}" />
<Binding Path="Height" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=Thumb}" />
</MultiBinding>
</LineSegment.Point>
</LineSegment>
<LineSegment>
<LineSegment.Point>
<MultiBinding Converter="{StaticResource thumb2PointConverter}" ConverterParameter="leftBottom">
<Binding Path="Width" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=Thumb}" />
<Binding Path="Height" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=Thumb}" />
</MultiBinding>
</LineSegment.Point>
</LineSegment>
</PathFigure>
</PathGeometry>
</Path.Data>
</Path>
</ControlTemplate>
Related
In WPF if we create multiple rectangles using item-container , then rectangles are getting overlapped on each other with inconsistent gaps?
Here you can see from 0-100 rectangle should be separated consistently but that didn't happened and then from 120-200 you can observe overlapping and shades.
**Note: All the values coming from converters are perfectly fine. So converters are not the issue.
I tried different ways like using SnapDeviceToPixels and RenderTransform too but it didn't worked
`
<ItemsControl ItemsSource="{Binding HighLightRegions}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas Height="{Binding ActualHeight, ElementName=myRectangle}"
Width="{Binding ActualWidth, ElementName=myRectangle}"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.Style>
<Style>
<Setter Property="Canvas.Right" Value="0"/>
<Setter Property="Canvas.Bottom" Value="0"/>
</Style>
</ItemsControl.Style>
<ItemsControl.ItemContainerStyle >
<Style>
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate DataType="plotLineViewModel:HighLightRectType">
<Canvas Height="{Binding ActualHeight, ElementName=PlotArea}">
<Rectangle Fill= "{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ItemsControl}}, Path=DataContext.FrameOverlayColor}"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Opacity=".6" Stroke="{StaticResource MyChartPlotBackgroundBrush}"
Height ="{Binding Path=ActualHeight, ElementName=myRectangle}">
<Canvas.Left>
<MultiBinding Converter="{StaticResource ChartViewXCordinateConverter}" >
<Binding Path="ActualWidth" ElementName="myRectangle" />
<Binding Path="Location.X" />
<Binding Path="DataContext.XAxisMax" ElementName="XPosition"/>
<Binding Path="DataContext.XAxisMin" ElementName="XPosition"/>
</MultiBinding>
</Canvas.Left>
<Rectangle.Width>
<MultiBinding Converter="{StaticResource ChartViewXCordinateConverter}" >
<Binding Path="ActualWidth" ElementName="myRectangle" />
<Binding Path="Width" />
<Binding Path="DataContext.XAxisMax" ElementName="XPosition" />
<Binding Path="DataContext.XAxisMin" ElementName="XPosition"/>
</MultiBinding>
</Rectangle.Width>
</Rectangle>
</Canvas>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
`
I have strange error in the Visual Studio at line with <ControlTemplate TargetType="ItemsControl">
Error XDG0062 'System.Windows.Media.RectangleGeometry' is not a valid value for property 'Children'.
I suspect that I don't see a control preview due to this error.
At runtime, everything looks OK. Should I worry about it, or ignore it? Maybe I should do something different?
<ItemsControl>
<ItemsControl.Template>
<ControlTemplate TargetType="ItemsControl">
<Canvas x:Name="m_Canvas">
<Canvas.Resources>
<RectangleGeometry x:Key="RectangleGeometry1">
<RectangleGeometry.Rect>
<MultiBinding Converter="{StaticResource MyRectConverter}">
<Binding Path="ActualWidth" ElementName="m_Canvas" />
<Binding Path="ActualHeight" ElementName="m_Canvas" />
</MultiBinding>
</RectangleGeometry.Rect>
</RectangleGeometry>
<RectangleGeometry x:Key="RectangleGeometry2">
<RectangleGeometry.Rect>
<MultiBinding Converter="{StaticResource MyRectConverter}">
<Binding Path="ActualWidth" ElementName="m_Canvas" />
<Binding Path="ActualHeight" ElementName="m_Canvas" />
</MultiBinding>
</RectangleGeometry.Rect>
</RectangleGeometry>
</Canvas.Resources>
<Path Fill="#35A500"
Opacity="0.15">
<Path.Data>
<GeometryGroup FillRule="EvenOdd">
<StaticResource ResourceKey="RectangleGeometry1" />
<StaticResource ResourceKey="RectangleGeometry2" />
</GeometryGroup>
</Path.Data>
</Path>
</Canvas>
</ControlTemplate>
</ItemsControl.Template>
</ItemsControl>
I'm creating a Wizard control based on Selector. My plan is that it will behave in the similar way to TabControl, ListBox, etc. - by having its own child items.
What I already did is:
public class Wizard : Selector
{
// ...
protected override DependencyObject GetContainerForItemOverride() => new WizardPage();
protected override bool IsItemItsOwnContainerOverride(object item) => item is WizardPage;
// ...
The relevant part ControlTemplate for my control looks like this:
<Style TargetType="{x:Type c:Wizard}">
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<Grid />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type c:Wizard}">
<DockPanel Background="{TemplateBinding Background}"
Margin="{TemplateBinding Padding}">
<StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal" HorizontalAlignment="Right">
<Button Name="BackButton" MinWidth="75" Command="{x:Static c:WizardCommands.PreviousPage}"
Content="{TemplateBinding BackButtonContent}" Style="{StaticResource WizardButton}">
<Button.Visibility>
<MultiBinding Converter="{StaticResource WizardPageButtonVisibilityConverter}">
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="BackButtonVisibility" />
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="SelectedItem.BackButtonVisibility" />
</MultiBinding>
</Button.Visibility>
</Button>
<Button Name="NextButton" Command="{x:Static c:WizardCommands.NextPage}"
Content="{TemplateBinding NextButtonContent}" Style="{StaticResource WizardButton}">
<Button.Visibility>
<MultiBinding Converter="{StaticResource WizardPageButtonVisibilityConverter}">
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="NextButtonVisibility" />
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="SelectedItem.NextButtonVisibility" />
</MultiBinding>
</Button.Visibility>
</Button>
<Button Name="FinishButton" Command="{x:Static c:WizardCommands.Finish}" Content="{TemplateBinding FinishButtonContent}"
Style="{StaticResource WizardButton}">
<Button.Visibility>
<MultiBinding Converter="{StaticResource WizardPageButtonVisibilityConverter}">
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="FinishButtonVisibility" />
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="SelectedItem.FinishButtonVisibility" />
</MultiBinding>
</Button.Visibility>
</Button>
<Button Name="CancelButton" Command="{x:Static c:WizardCommands.Cancel}" Content="{TemplateBinding CancelButtonContent}"
Style="{StaticResource WizardButton}">
<Button.Visibility>
<MultiBinding Converter="{StaticResource WizardPageButtonVisibilityConverter}">
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="CancelButtonVisibility" />
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="SelectedItem.CancelButtonVisibility" />
</MultiBinding>
</Button.Visibility>
</Button>
</StackPanel>
<ContentPresenter Content="{Binding SelectedItem, RelativeSource={RelativeSource TemplatedParent}}" />
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
However, when I assign an ObservableCollection<BaseViewModel> along with specific DataTemplates...
<c:Wizard Grid.Column="1" ItemsSource="{Binding WizardSteps}">
<c:Wizard.Resources>
<DataTemplate DataType="{x:Type vm:WizardStep1ViewModel}">
<Border Background="Red" />
</DataTemplate>
<DataTemplate DataType="{x:Type vm:WizardStep2ViewModel}">
<Border Background="Green" />
</DataTemplate>
<DataTemplate DataType="{x:Type vm:WizardStep3ViewModel}">
<Border Background="Blue" />
</DataTemplate>
</c:Wizard.Resources>
</c:Wizard>
...then the DataTemplates are instantiated and shown properly, but no WizardPages are being created in the meantime.
How can I fix it?
Your ControlTemplate should include an ItemsPresenter for a WizardPage to get created for each item in the source collection, e.g.:
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="c:Wizard">
<ItemsPresenter />
</ControlTemplate>
</Setter.Value>
</Setter>
I'm trying to change the background of the selected item in a WPF ListBox.
I have attempted to implement a style for it, but for some reason it's not being applied. I still get a blue background. Can anyone see why?
<UserControl x:Class="Thumbnails"
xmlns:local="clr-namespace:ContentPresenter"
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"
mc:Ignorable="d"
d:DesignHeight="350" d:DesignWidth="800">
<UserControl.Resources>
<local:ThumbImageHeightConverter x:Key="HeightConv" />
<local:ThumbImageWidthConverter x:Key="WidthConv" />
<local:InnerGridHeightConverter x:Key="InnerGridHeightConv" />
<local:ReflectWidthConverter x:Key="ReflectWidthConv" />
<local:ReflectCenterYConv x:Key="ReflectCenterYConv" />
<Style x:Name="ListBoxItemStyle" TargetType="{x:Type ListBoxItem}">
<Style.Triggers>
<Trigger Property="Selector.IsSelected" Value="True">
<Setter Property="Background" Value="White" />
</Trigger>
</Style.Triggers>
</Style>
<Style TargetType="{x:Type ListBox}">
<!-- Set the ItemTemplate of the ListBox to a DataTemplate which explains how to display an object of type BitmapImage. -->
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<Grid x:Name="ThumbGrid" VerticalAlignment="Top" Height="{Binding ElementName=ThumbListBox, Path=ActualHeight}" >
<Grid.RowDefinitions>
<RowDefinition x:Name="ThumbGridThumbImgRow" ></RowDefinition>
<RowDefinition x:Name="GridReflectRow" Height="40" ></RowDefinition>
</Grid.RowDefinitions>
<Border x:Name="HighlightBorder" BorderThickness="7" BorderBrush="Black" CornerRadius="18" Padding="2" Margin="4" HorizontalAlignment="Center" VerticalAlignment="Center">
<Grid x:Name="ThumbInnerGrid">
<Grid.Height>
<MultiBinding Converter="{StaticResource InnerGridHeightConv}">
<Binding ElementName="ThumbGrid" Path="ActualHeight" />
<Binding ElementName="HighlightBorder" Path="CornerRadius" />
<Binding ElementName="mask" Path="CornerRadius" />
</MultiBinding>
</Grid.Height>
<Border x:Name="mask" Background="White" CornerRadius="15" />
<StackPanel x:Name="ThumbInnerStack" >
<StackPanel.OpacityMask>
<VisualBrush Visual="{Binding ElementName=mask}"/>
</StackPanel.OpacityMask>
<!--<Image x:Name="ThumbImg" Source="{Binding Path=UriSource}" Stretch="Fill" Width="{Binding}" Height="{Binding Source={StaticResource ThumbImageSize}, Path=ImgHeight}">-->
<Image x:Name="ThumbImg" Stretch="UniformToFill" SnapsToDevicePixels="True" >
<Image.Height>
<MultiBinding Converter="{StaticResource HeightConv}">
<Binding ElementName="HighlightBorder" Path="ActualHeight" />
<Binding ElementName="HighlightBorder" Path="BorderThickness" />
<Binding ElementName="HighlightBorder" Path="Padding" />
</MultiBinding>
</Image.Height>
<Image.Width>
<MultiBinding Converter="{StaticResource WidthConv}">
<Binding ElementName="ThumbImg" Path="ActualHeight" />
<Binding ElementName="HighlightBorder" Path="BorderThickness" />
<Binding ElementName="HighlightBorder" Path="Padding" />
</MultiBinding>
</Image.Width>
<Image.Source>
<BitmapImage UriSource="{Binding Path=Src}"></BitmapImage>
</Image.Source>
</Image>
</StackPanel>
</Grid>
</Border>
<Border Height="{Binding ElementName=ThumbImg, Path=ActualHeight}" Grid.Row="1" CornerRadius="15" SnapsToDevicePixels="True" Opacity="0.75" >
<Border.Width>
<MultiBinding Converter="{StaticResource ReflectWidthConv}">
<Binding ElementName="HighlightBorder" Path="ActualWidth" />
<Binding ElementName="HighlightBorder" Path="BorderThickness" />
</MultiBinding>
</Border.Width>
<Border.Background>
<VisualBrush Visual="{Binding ElementName=ThumbImg}">
<VisualBrush.Transform>
<ScaleTransform ScaleX="1" ScaleY="-1" CenterX="200">
<ScaleTransform.CenterY>
<MultiBinding Converter="{StaticResource ReflectCenterYConv}">
<Binding ElementName="ThumbImg" Path="ActualHeight" />
</MultiBinding>
</ScaleTransform.CenterY>
</ScaleTransform>
</VisualBrush.Transform>
</VisualBrush>
</Border.Background>
<Border.OpacityMask>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1.3">
<GradientStop Offset="0" Color="Black"></GradientStop>
<GradientStop Offset="0.1" Color="Transparent"></GradientStop>
</LinearGradientBrush>
</Border.OpacityMask>
</Border>
<!--<Label x:Name="ThumbTitle" Grid.Row="1" Content="{Binding Path=Title}" HorizontalAlignment="Center"></Label>-->
<Label x:Name="ThumbTitle" Grid.Row="1" Content="{Binding ElementName=ThumbInnerGrid, Path=ActualHeight, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Center"></Label>
</Grid>
</DataTemplate>
</Setter.Value>
</Setter>
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" />
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Disabled" />
<Setter Property="Background" Value="Black"/>
</Style>
</UserControl.Resources>
<UserControl.DataContext>
<ObjectDataProvider ObjectType="{x:Type local:ThumbImageLoader}" MethodName="LoadImagesv2" IsAsynchronous="True" />
</UserControl.DataContext>
<!-- This ListBox is the Content of the Window. Normally you would have a panel of some type as the Window's Content, but let's keep it simple. -->
<Grid x:Name="ThumbListBoxGrid">
<ListBox x:Name="ThumbListBox" ItemsSource="{Binding}" VerticalAlignment="Top" Height="{Binding ElementName=ThumbListBoxGrid, Path=ActualHeight}" IsSynchronizedWithCurrentItem="True" />
</Grid>
Anyone see what's wrong here?
You specify the SelectedItem Background for a ListBox with the SystemColors.HighlightBrushKey (focused) and SystemColors.ControlBrushKey (not focused)
<Style TargetType="{x:Type ListBox}">
<Style.Resources>
<!-- Background of selected item when focussed -->
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
Color="White"/>
<!-- Background of selected item when not focussed -->
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}"
Color="White" />
</Style.Resources>
<!--...-->
</Style>
For anyone searching for this in the future, the accepted answer doesn't actually apply the colour when control is not focused for me. The following should be used instead which appears to work as intended.
<Style TargetType="ListBox">
<Style.Resources>
<!-- Background of selected item when focussed -->
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#FFFFB733" />
<!-- Background of selected item when not focussed -->
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="#FFFFB733"/>
</Style.Resources>
</Style>
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).
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>