How to select tabs with AvalonDock - wpf

I'm trying to select tabs (both in a LayoutDocumentPaneGroup and LayoutAnchorablePane) in AvalonDock. This seems like it should be an easy task, but I'm struggling to find any documentation on the subject. So far the best I've gotten is the ability to select the initial tab (see below), but this binding doesn't seem to persist when changing the bound property after the initial load.
<dock:DockingManager Name="DockingManager" Grid.Row="2"
AnchorablesSource="{Binding Anchorables}"
DocumentsSource="{Binding Documents}"
DocumentClosed="DockingManager_DocumentClosed"
DocumentClosing="DockingManager_DocumentClosing"
Loaded="DockingManager_Loaded"
MouseUp="DockingManager_MouseUp">
<dock:DockingManager.LayoutItemContainerStyle>
<Style TargetType="{x:Type dockctrl:LayoutItem}" >
<Setter Property="Title" Value="{Binding Model.Title}" />
<Setter Property="CloseCommand" Value="{Binding Model.CloseCommand}" />
<Setter Property="CanClose" Value="{Binding Model.CanClose}" />
<Setter Property="IsSelected" Value="False" />
<Style.Triggers>
<DataTrigger Binding="{Binding Model.Title}" Value="Resources">
<Setter Property="IsSelected" Value="True" />
</DataTrigger>
</Style.Triggers>
</Style>
</dock:DockingManager.LayoutItemContainerStyle>
<dock:LayoutRoot>
<dock:LayoutPanel Orientation="Horizontal">
<dock:LayoutAnchorablePaneGroup x:Name="leftAnchorableGroup" DockWidth="300" >
<dock:LayoutAnchorablePane />
</dock:LayoutAnchorablePaneGroup>
<dock:LayoutPanel Orientation="Vertical">
<dock:LayoutPanel Orientation="Horizontal">
<dock:LayoutDocumentPaneGroup x:Name="leftDocumentGroup">
<dock:LayoutDocumentPane />
</dock:LayoutDocumentPaneGroup>
</dock:LayoutPanel>
</dock:LayoutPanel>
</dock:LayoutPanel>
</dock:LayoutRoot>
</dock:DockingManager>
However if I replace these lines:
<Setter Property="IsSelected" Value="False" />
<Style.Triggers>
<DataTrigger Binding="{Binding Model.Title}" Value="Resources">
<Setter Property="IsSelected" Value="True" />
</DataTrigger>
</Style.Triggers>
with:
<Setter Property="IsSelected" Value="{Binding Model.ContentIsSelected" />
...it doesn't work when I change the value of ContentIsSelected. I can see (using Snoop) that the value of ContentIsSelected itself i in fact changing but IsSelected Does not change with it?!
I also found this other question (which lead me down the path of trying to use IsSelected): How to switch between document tabs in AvalonDock 2 However 'm not entirely sure how to programatically access the LayoutItems beyond the bindings in XAML. I tried the DockingManager.GetLayoutItemFromModel() function but could not get it to return anything other than NULL.
How can I select a tab and bring it into view/focus (as if I were clicking the tab with a mouse)?

The solution ended up being that the default binding was not as expected.
<Setter Property="IsSelected" Value="{Binding Model.ContentIsSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

Related

How to fix Xceed DateTimePicker ArgumentOutOfRangeException Error

I am using the Xceed DateTimePicker as the control in a wpf DataGrid for all columns that are bound to a Date property. Each of the those columns is defined as follows:
<DataGrid.Columns>
<DataGridTemplateColumn
Header="Charge Date"
Width="100">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock
Text="{Binding Path=ChargeDate, StringFormat=yyyy-MM-dd, Converter={StaticResource conDate}}"
HorizontalAlignment="Center" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<tk:DateTimePicker Value="{Binding Path=ChargeDate}" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
I don't think it is pertinent to my question, but for completeness sake, here is the styling for the pickers:
<Style TargetType="{x:Type tk:DateTimePicker}">
<Setter Property="TextAlignment" Value="Center" />
<Setter Property="Minimum" Value="2017-01-01" />
<Setter Property="DisplayDefaultValueOnEmptyText" Value="False" />
<Setter Property="ShowButtonSpinner" Value="False" />
<Setter Property="TimePickerVisibility" Value="Collapsed" />
<Setter Property="AutoCloseCalendar" Value="True" />
<Setter Property="Format" Value="Custom" />
<Setter Property="FormatString" Value="yyyy-MM-dd" />
</Style>
This works for datagrid cells that are already populated or when I am entering data in a new row. However when I click on an empty cell in an existing row, I get the following exception:
System.ArgumentOutOfRangeException: 'SelectedDate value is not valid.'
Why is the error only when I am enter data in an existing row? No code-behind is being executed when this exception happens so I don't know where to look for the problem.
I had the same issue. To fix this set the ClipValueToMinMax property to "True". This will prevent the value from going below minimum/above maximum without throwing an exception.
<xceed:DateTimePicker Value="{Binding DateTime}"
Minimum="{Binding DateTimeMinimum}"
Maximum="{Binding DateTimeMaximum}"
ClipValueToMinMax="True"/>
Since you have set the Minimum property to 2017-01-01, you should also set the default value to the same date:
<Style TargetType="{x:Type tk:DateTimePicker}">
<Setter Property="TextAlignment" Value="Center" />
<Setter Property="Default" Value="2017-01-01" />
<Setter Property="Minimum" Value="2017-01-01" />
<Setter Property="DisplayDefaultValueOnEmptyText" Value="False" />
<Setter Property="ShowButtonSpinner" Value="False" />
<Setter Property="TimePickerVisibility" Value="Collapsed" />
<Setter Property="AutoCloseCalendar" Value="True" />
<Setter Property="Format" Value="Custom" />
<Setter Property="FormatString" Value="yyyy-MM-dd" />
</Style>
You will get an ArgumentOutOfRangeException if the default value or value is less than the minimum value and this makes perfect sense.

Validation using IDataErrorInfo

I'm using IDataErrorInfo to implement some basic logic validation for some values. This seems to work well, and I'm using a contentpresenter to display the results:
<ContentPresenter Content="{ Binding ElementName =MyElement, Path=(Validation.Errors).CurrentItem}"
HorizontalAlignment ="Left">
<ContentPresenter.ContentTemplate>
<DataTemplate>
<TextBlock Foreground ="Red" FontStyle="Italic" Text="{ Binding Path =ErrorContent}" />
</DataTemplate>
</ContentPresenter.ContentTemplate>
</ContentPresenter>
I get a nice red message when there's a problem, and the field in question is correctly highlighted. However, when this happens, I'd like to disable the save button for the form. Here's what I've tried so far (without success):
<Button Content="Save" Click ="MyButton_Click"
IsEnabled="{Binding Converter={StaticResource ValidConverter}, ConverterParameter={Binding ElementName=MyElement, Path=(Validation.Errors).CurrentItem}}"/>
ValidConverter is just a converter that returns true for a null or empty string.
I also tried triggers, like this (tried both Trigger and DataTrigger):
<Button Content="Save" Click ="MyButton_Click"
<Button.Style>
<Style>
<Style.Triggers>
<Setter Property ="Button.IsEnabled" Value="True" />
<DataTrigger Binding ="{ Binding Path=(Validation.HasError)}" Value ="True">
<Setter Property ="Button.IsEnabled" Value="False" />
</ DataTrigger>
</Style.Triggers>
</Style>
</ Button.Style>
</ Button>
I've found some on-line information on this, and as far as I can tell the trigger method should work; however, if I use Trigger, nothing happens, and DataTrigger doesn't compile (error MC1000: Unknown build error, 'Index (zero based) must be greater than or equal to zero and less than the size of the argument list).
Can anyone tell me why this doesn't work, and what I'm doing wrong here?
Hmm ... I'm doing it the other way around ... here's a similar button, but it's based on several validations for several TextBoxs and ComboBox that need to be validated.
So, I'll set it to false by default, and when all the validations errors are false, set it to true.
<Button x:Name="Btn_Insert"
Command="{Binding Insert_Command}"
IsDefault="True"
>
<Button.Style>
<Style TargetType="Button">
<Setter Property="IsEnabled" Value="False"/>
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Path=(Validation.HasError),
ElementName=txtbx_1}" Value="False"/>
<Condition Binding="{Binding Path=(Validation.HasError),
ElementName=cmb_1}" Value="False"/>
<Condition Binding="{Binding Path=(Validation.HasError),
ElementName=txtbx_2}" Value="False"/>
<Condition Binding="{Binding Path=(Validation.HasError),
ElementName=txtbx_3}" Value="False"/>
</MultiDataTrigger.Conditions>
<Setter Property="IsEnabled" Value="True"/>
</MultiDataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
Try also putting your <Setter Property ="Button.IsEnabled" Value="True" /> above the <Style.Triggers>
The problem was with the default:
<Style.Triggers>
<DataTrigger Binding ="{ Binding Path=(Validation.HasError)}" Value ="True">
<Setter Property ="Button.IsEnabled" Value="False" />
</ DataTrigger>
</Style.Triggers>
Works fine. As #Shoe pointed out - there was a typo in the question - now corrected.

How to use the Calendar's Display Mode as a DataTrigger's Value

So I need to know how to set up a xmlns to let me use the CalendarMode in a Trigger's value.
I have tried using xmlns:cal="clr-namespace:System.Windows.Controls", xmlns:cal="clr-namespace:System.Windows.Controls.Calendar" and I've built the project each time, but I got error telling me that the CLR namespace is undefined and cannot be found.
Here is where I used it
<DataTrigger Binding="{Binding Source=_Calendar, Path=Calendar.DisplayMode}">
<DataTrigger.Value>
<cal:CalendarMode>Month</cal:CalendarMode>
</DataTrigger.Value>
<Setter Property="Grid.Opacity" Value="1" />
</DataTrigger>
I guess I could just listen to the DisplayModeChanged event on the calendar but since I've been searching online for this solution all day, I'd really like to know how I can approach this problem in this way.
Any input will be highly appreciated. Thanks!
actually I did not understand exactly what you need. But I'll try to help.
the definition we see:
then able to use the xaml we have to do:
xmlns:presentation="clr-namespace:System.Windows.Controls;assembly=PresentationFramework"
Now, if you want something to happen with a dependency property of the own control, you should use Triggers and not DataTriggers
Sample:
<Calendar Height="170" HorizontalAlignment="Left" Margin="83,112,0,0" Name="calendar1" VerticalAlignment="Top" Width="180">
<Calendar.Style>
<Style TargetType="Calendar">
<Setter Property="Opacity" Value="0.4"/>
<Style.Triggers>
<Trigger Property="SelectionMode" Value="{x:Static presentation:CalendarMode.Month}">
<Setter Property="Opacity" Value="1.0"/>
</Trigger>
</Style.Triggers>
</Style>
</Calendar.Style>
</Calendar>
Normally DataTriggers are used for objects created by you, which implementational INotifyPropertyChanged. Do not mess.
Now, if you want to change another control (when CalendarMode changes) you should do:
<Calendar Height="170" HorizontalAlignment="Left" Margin="83,112,0,0"
Name="calendar1" VerticalAlignment="Top" Width="180"/>
<Grid>
<Grid.Style>
<Style TargetType="Grid">
<Setter Property="Opacity" Value="0.5"/>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=calendar1, Path=CalendarMode}">
<DataTrigger.Value>
<presentation:CalendarMode>Month</presentation:CalendarMode>
</DataTrigger.Value>
<Setter Property="Opacity" Value="1.0"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Grid.Style>
</Grid>
I suggest you read more about triggers, datatriggers and bindings.

Why can't I add a DataTrigger to my control's Triggers collection?

Why cant I code like this
<Border Width="130" Height="70">
<Border.Triggers>
<DataTrigger Binding="{Binding Path=CurrentStatus}" Value="0">
<Setter Property="Style" Value="{StaticResource ResourceKey=ListBoxItemBorder}"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=CurrentStatus}" Value="200">
<Setter Property="Style" Value="{StaticResource ResourceKey=ListBoxItemBorderInactive}"/>
</DataTrigger>
</Border.Triggers>
</Border>
I get this error
Failed object initialization (ISupportInitialize.EndInit).
Triggers collection members must be of type EventTrigger.
Error at object '4_T' in markup file
What am I doing wrong plz help.
Abe is correct and explains the limitations well. One thing you might want to consider is:
Instead of having two border styles, and trying to pick between them based on a trigger...
Use a single style on your border, this style's setters represent your 'normal' look.
This style also contains your DataTrigger, and your DataTrigger has a collection of setters which essentially represents your second style (which have higher priority than the standard setters when this trigger evaluates to true!
Edit:
Something like this -
<Style TargetType="Border" x:Key="BorderStyle">
<!-- These setters are the same as your normal style when none of your triggers are true -->
<Setter Property="BorderBrush" Value="Black" />
<Style.Triggers>
<DataTrigger Binding="{Binding Path=CurrentStatus}" Value="0">
<!-- These setters are the same as your ListBoxItemBorder style -->
<Setter Property="BorderBrush" Value="Green" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=CurrentStatus}" Value="200">
<!-- These setters are the same as your ListBoxItemBorderInactive style -->
<Setter Property="BorderBrush" Value="Gray" />
</DataTrigger>
</Style.Triggers>
</Style>
Unfortunately, only EventTriggers can be applied directly to elements. If you want to use a Trigger or DataTrigger, they have to be in a Style, ControlTemplate, or DataTemplate.
From the resource names, it looks like this is a Border inside a ListBoxItem ControlTemplate. You could easily move the triggers into the template's triggers collection.
Here is a way for no limitations triggers.
Example:
<Border Width="130" Height="100" Grid.Row="1">
<ListBox x:Name="lstItems" ItemsSource="{Binding TestItems}">
</ListBox>
<tg:TriggerExtensions.Triggers>
<tg:TriggerCollections>
<tg:DataTriggerInfo Binding="{Binding CurrentStatus}" Value="0">
<tg:DataTriggerInfo.Setters>
<tg:SetterInfo ElementName="lstItems" Property="Style" Value="{StaticResource ListBoxRed}"/>
</tg:DataTriggerInfo.Setters>
</tg:DataTriggerInfo>
<tg:DataTriggerInfo Binding="{Binding CurrentStatus}" Value="0" IsInvert="True">
<tg:DataTriggerInfo.Setters>
<tg:SetterInfo ElementName="lstItems" Property="Style" Value="{StaticResource ListBoxBlue}"/>
</tg:DataTriggerInfo.Setters>
</tg:DataTriggerInfo>
</tg:TriggerCollections>
</tg:TriggerExtensions.Triggers>
</Border>
Link Sample
Link Component Github

Underline Label in WPF, using styles

I have the following style:
<Style x:Key="ActionLabelStyle" TargetType="{x:Type Label}">
<Setter Property="Margin" Value="10,3" />
<Setter Property="Padding" Value="0" />
<Setter Property="TextBlock.TextWrapping" Value="Wrap" />
<Setter Property="FontFamily" Value="Calibri" />
<Style.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsMouseOver" Value="True" />
<Condition Property="IsEnabled" Value="True" />
</MultiTrigger.Conditions>
<Setter Property="Background" Value="Red" />
<Setter Property="TextBlock.TextDecorations" Value="Underline" />
</MultiTrigger>
</Style.Triggers>
</Style>
So basically, I want to have a label which is underlined when it is enabled and the mouse cursor is over it. The part of this style which is not working is the <Setter Property="TextBlock.TextDecorations" Value="Underline" />. Now, what am I doing wrong here? Thanks for all the help.
This is actually much more difficult than it appears. In WPF, a Label is not a TextBlock. It derives from ContentControl and can therefore host other, non-text controls in its Content collection.
However, you can specify a string as the content as in the example below. Internally, a TextBlock will be constructed to host the text for you.
<Label Content="Test!"/>
This internally translates to:
<Label>
<Label.Content>
<TextBlock>
Test!
</TextBlock>
</Label.Content>
</Label>
The simple solution to this would be for the TextDecorations property of a TextBlock to be an attached property. For example, FontSize is designed this way, so the following works:
<Label TextBlock.FontSize="24">
<Label.Content>
<TextBlock>
Test!
</TextBlock>
</Label.Content>
</Label>
The TextBlock.FontSize attached property can be applied anywhere in the visual tree and will override the default value for that property on any TextBlock descendant in the tree. However, the TextDecorations property is not designed this way.
This leaves you with at least a few options.
Use color, border, cursor, etc., instead of underlined text because this is 100% easier to implement.
Change the way you are doing this to apply the Style to the TextBlock instead.
Go to the trouble to create your own attached property and the control template to respect it.
Do something like the following to nest the style for TextBlocks that appear as children of your style:
FYI, this is the ugliest thing I've done in WPF so far, but it works!
<Style x:Key="ActionLabelStyle" TargetType="{x:Type Label}">
<Setter Property="Margin" Value="10,3" />
<Setter Property="Padding" Value="0" />
<Setter Property="TextBlock.TextWrapping" Value="Wrap" />
<Setter Property="FontFamily" Value="Calibri" />
<Style.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsMouseOver" Value="True" />
<Condition Property="IsEnabled" Value="True" />
</MultiTrigger.Conditions>
<Setter Property="Background" Value="Red" />
</MultiTrigger>
</Style.Triggers>
<Style.Resources>
<Style TargetType="TextBlock">
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Label}, Path=IsMouseOver}" Value="True" />
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsEnabled}" Value="True" />
</MultiDataTrigger.Conditions>
<Setter Property="TextDecorations" Value="Underline"/>
</MultiDataTrigger>
</Style.Triggers>
</Style>
</Style.Resources>
</Style>
This works because it is overriding the default style of any TextBlock beneath a Label of this style. It then uses a MultiDataTrigger to allow relative binding back up to the Label to check if its IsMouseOver property is True. Yuck.
Edit:
Note that this only works if you explicitly create the TextBlock. I was incorrect when I posted this because I had already dirtied up my test Label. Boo. Thanks, Anvaka, for pointing this out.
<Label Style="{StaticResource ActionLabelStyle}">
<TextBlock>Test!</TextBlock>
</Label>
This works, but if you have to go to this trouble, you're just working too hard. Either someone will post something more clever, or as you said, my option 1 is looking pretty good right now.
Further to Jerry's answer, in order to avoid having to add the TextBlock into the template each time you can let the style do this too by adding the Setter property into the style:
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Label}">
<TextBlock>
<ContentPresenter />
</TextBlock>
</ControlTemplate>
</Setter.Value>
</Setter>
Then your Label is back to:
<Label Content="Test!" Style="{StaticResource ActionLabelStyle}" />
Thanks Jerry!
Andrew.
I think the issue is that TextBlock.TextDecorations is not defined on Label.
You can use this approach if you're happy to use a TextBlock rather than a Label.
Just to add my workaround to the mix. I am currently using C# code and it works well enough. I just trap the MouseLeave and MouseEnter events and show the underline there.
void Control_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
WPFHelper.EnumerateChildren<TextBlock>(this, true).ForEach(c => c.TextDecorations = null);
}
void Control_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
WPFHelper.EnumerateChildren<TextBlock>(this, true).ForEach(c => c.TextDecorations = TextDecorations.Underline);
}
The WPFHelper class simply enumerates all the children of an DependencyObject and ForEach is an extension method that just does executes the action inside the lambda expression for each item.
An old question, but since I just fought with this, heres my method. Though it just uses a Trigger as opposed to a MultiTrigger
For XAML:
<Label Content="This text is for testing purposes only.">
<Style TargetType="{x:Type ContentPresenter}">
<Style.Resources>
<Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource {x:Type TextBlock}}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="TextDecorations" Value="Underline" />
</Trigger>
</Style.Triggers>
</Style>
</Style.Resources>
</Style>
</Label>
For C# Codebehind:
public override void OnApplyTemplate()
{
if( !hasInitialized )
{
var tbUnderStyle = new Style( typeof(TextBlock), (Style)FindResource(typeof(TextBlock)) );
var tbUnderSetter = new Setter( TextBlock.TextDecorationsProperty, TextDecorations.Underline );
var tbUnderTrigger = new Trigger() { Property = Label.IsMouseOverProperty, Value = true };
tbUnderTrigger.Setters.Add( tbUnderSetter );
var contentPresenter = FindVisualChild<ContentPresenter>( this );
contentPresenter.Resources.Add( typeof(TextBlock), tbUnderStyle );
hasInitialized = true;
}
}
hasInitialized being a bool that is set to false in the constructor, and FindVisualChild sourced from here.

Resources