How to fix Xceed DateTimePicker ArgumentOutOfRangeException Error - wpf

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.

Related

How to select tabs with AvalonDock

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}" />

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 do styling of data grid?

here i want to give alternate color white and grey to grid row . i hv done many try but i can not do styling of grid .the code is here
<Style TargetType="{x:Type wpftoolkit:DataGrid}">
<Setter Property="Margin" Value="0" />
<Setter Property="BorderBrush" Value="#A6A6A6" />
<Setter Property="BorderThickness" Value="0,1,0,0"/>
<Setter Property="Background" Value="{StaticResource GridBgBrush}" />
<Setter Property="RowBackground" Value="White" />
<Setter Property="AlternatingRowBackground" Value="#FFF3F6FA" />
<Setter Property="GridLinesVisibility" Value="Horizontal" />
<Setter Property="HorizontalGridLinesBrush" Value="Transparent" />
<Setter Property="RowHeaderWidth" Value="0" />
</Style>
here StaticResource GridBgBrush define earlier on this file as`
plz give proper solution .thanks in advance.
Make sure that your style is either defined within the resources section of your XAML file (after your GridBgBrush, so that it can reference it), or in a ResourceDictionary in your App somewhere making it accessible from anywhere. Without seeing more, I can't tell you where your problem is coming from. That is the correct way to define your style and I have several examples of this working as expected if you're interested in seeing them.
Another thing to note in case you didn't know, is that DataGrid (along with DatePicker) was introduced into WPF v4.0. This makes the WPF Toolkit (at least for the purposes of the DataGrid) unnecessary if you can target that version. After saying that, I suppose there's the slight chance that if you weren't aware you were using one and then styling the other, your style wouldn't work.
<XmlDataProvider x:Key="myData" Source="Data.xml" IsAsynchronous="True" />
<Style TargetType="{x:Type DataGrid}" x:Key="myStyle">
<Setter Property="AlternatingRowBackground" Value="Red"/>
</Style>
<Grid>
<DataGrid ItemsSource="{Binding Source={StaticResource myData}, XPath=persons/person}" AutoGenerateColumns="False" Style="{StaticResource myStyle}">
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding XPath=firstname}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding XPath=lastname}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
You need to set AlternationCount property too.

Change ListView.ItemTemplate on subelement change

let's say we have simple data class:
public class Ex {
public string Prop1 {...} // notify property
public string Prop2 {...} // notify property
}
and an ObservableCollection of objects of this class. I want to have this collection displayed in a ListView with seperated DataTemplated which is distinguished by Ex.Prop2 (if it is null or empty then template01 is used, otherwise template02). This property can be changed in runtime so simple "trick" with ListView.ItemTemplateSelector does not work :(
How to achieve this functionality? Is it possible to achieve it any other way than listening to NotifyPropertyChanged on each object of the collection and than changing manually the template?
Thanks for your help.
Below piece of code which I already have:
<ListView x:Name="lstTerms"
ItemsSource="{Binding Game.Words}"
HorizontalContentAlignment="Stretch"
Grid.IsSharedSizeScope="True">
<ListView.ItemContainerStyle>
<Style>
<Setter Property="Control.Padding" Value="0" />
</Style>
</ListView.ItemContainerStyle>
<!-- checks if element is null or its Prop2 is null or empty. If so, uses NullTemplate -->
<ListView.ItemTemplateSelector>
<local:MySelectTemplate
NormalTemplate="{StaticResource NormalItemTemplate}"
NullTemplate="{StaticResource NullItemTemplate}" />
</ListView.ItemTemplateSelector>
</ListView>
Instead of using a TemplateSelector, you can have a single DataTemplate containing two Grids, which switch visibility dependent on the property values.
Here is an example:
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid Background="LightBlue" Name="normalGrid">
<Grid.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Prop1}" Value="{x:Null}">
<Setter Property="Grid.Visibility" Value="Hidden"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</Grid.Style>
<TextBlock Text="{Binding Prop1}"></TextBlock>
</Grid>
<Grid Background="Green" Name="nullGrid">
<Grid.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=normalGrid, Path=Visibility}" Value="Visible">
<Setter Property="Grid.Visibility" Value="Hidden"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</Grid.Style>
<TextBlock Text="{Binding Prop2}"></TextBlock>
</Grid>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
Obviously you could replace the TextBlock elements with UserControls representing your two DataTemplates.
If you want, you can also remove the need for the bulky Styles by binding Grid.Visibility to a property (named, for example, IsVisible) on your ViewModel and using a VisibilityConverter.
I usually just use a ContentControl which changes its ContentTemplate based on a DataTrigger. DataTriggers respond to the value getting changed, while DataTemplateSelectors do not
<Style x:Key="SomeStyleKey" TargetType="{x:Type ContentControl}">
<Setter Property="ContentTemplate" Value="{StaticResource DefaultTemplate}" />
<Style.Triggers>
<DataTrigger Binding="{Binding Prop2}" Value="{x:Null}">
<Setter Property="ContentTemplate" Value="{StaticResource NullTemplate}" />
</DataTrigger>
<DataTrigger Binding="{Binding Prop2}" Value="">
<Setter Property="ContentTemplate" Value="{StaticResource NullTemplate}" />
</DataTrigger>
</Style.Triggers>
</Style>
...
<ListView.ItemTemplate>
<DataTemplate>
<ContentControl Style="{StaticResource SomeStyleKey}" />
</DataTemplate>
</ListView.ItemTemplate>
You could also use a Converter that returns String.IsNullOrEmpty(value) if you wanted a single DataTrigger

Can I get this binding into a style?

I would like to take the xaml I currently have for a ComboBox (below), and condense it into something like the Style also shown below. I think this should work, but I have a 'Type'ing issue and not sure how to resolve it
"Cannot resolve the Style Property 'Margin'. Verify that the owning type is the Style's TargetType, or use Class.Property syntax to specify the Property.)
As I look at the existing ComboBoxStyle (also below) that I'm looking to base this new style off of, I see that I hadn't used x:Type, but it does seem to work.
Is there any reason this new style shouldn't work? What must I change?
Cheers,
Berryl
combo box, as is, working):
<ComboBox
x:Name="cboDepartmentFilter" Style="{StaticResource ComboBoxStyle}"
Margin="{StaticResource FliterPanelItem_Margin}" Width="{StaticResource FilterPanelItem_Width}"
ItemsSource="{Binding Path=DepartmentFilterControl.Choices}"
ToolTip="{Binding DepartmentFilterControlData.ToolTipTitle}"
/>
what I want:
<ComboBox Style="{StaticResource FilterPanelComboBoxStyle}" DataContext="{Binding DepartmentFilterControl}" />
<!- in some resource file ->
<Style x:Key="FilterPanelComboBoxStyle" BasedOn="{StaticResource ComboBoxStyle}">
<Setter Property="Margin" Value="{StaticResource FliterPanelItem_Margin}" />
<Setter Property="Width" Value="{StaticResource FilterPanelItem_Width}" />
<Setter Property="ItemsSource" Value="{Binding Choices}" />
<Setter Property="ToolTip" Value="{Binding ToolTipTitle}" />
</Style>
<!--
This style defines a common margin for items in a filter panel.
-->
150
existing ComboBoxStyle:
<!-- ComboBox Style -->
<Style x:Key="ComboBoxStyle" TargetType="ComboBox">
<Setter Property="Background" Value="{StaticResource headerBrush}" />
...
<Setter Property="Template">
<Setter.Value>
...
</Setter.Value>
</Setter>
<Setter Property="ItemContainerStyle" Value="{StaticResource ComboBoxItemStyle}" />
<Setter Property="IsSynchronizedWithCurrentItem" Value="True" />
</Style>
You still need to specify the TargetType in the derived style. (Or you prefix the properties with "ComboBox.")

Resources