Is there a collection that I can use in my application where I can ignore the DataErrors? Currently, my BusinessObject implements the IDataErrorInfo interface, but I have a readonly control that I do not want to receive those notifications.
I tried using a DataTemplate with a TextBlock that has the property ValidatesOnDataErrors=False, but this didn't work.
Any ideas?
You could set the control's Validation.ErrorTemplate to null using a style.
<Style TargetType="Control">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="Validation.ErrorTemplate" Value="{x:Null}" />
</Trigger>
</Style.Triggers>
</Style>
Also, to clarify, the "ValidatesOnDataErrors" property should be used on the binding not the control itself.
The other alternative is to wrap your bound objects in some view model/adapter that doesn't implement IDataErrorInfo.
Related
I am currently using the code from this blogpost in order to have my TreeView highlight those items, which are currently hovered by the mouse. This is working as intended, however now I want the TreeViewItems to notify their attached ViewModels when they are hovered / not hovered.
However I'm at a loss on how I can achieve this. The corresponding XAML code looks like the following:
<Style TargetType="{x:Type TreeViewItem}">
<Style.Triggers>
<Trigger Property="Controls:TreeViewHelper.IsMouseDirectlyOverItem" Value="True">
<Setter Property="Background" Value="Green" />
</Trigger>
</Stile.Triggers>
</Style>
How can I bind the property from my ViewModel, named TreeNodeModel.IsHovered to the TreeViewItem (or probably the attached dependency property IsMouseDirectlyOverItem) so that I can react on those changes from within my code?
All the examples I found via google only explained how the set the background color. Thanks in advance for your time on the probably trivial answer.
In your Style, try adding a Setter which binds IsMouseDirectlyOverItem to IsHovered, and use the OneWayToSource binding mode to push the value the right way:
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="Controls:TreeViewHelper.IsMouseDirectlyOverItem"
Value="{Binding Path=IsHovered, Mode=OneWayToSource}" />
<Style.Triggers>
...
</Style>
EDIT: As IsMouseDirectlyOver is read-only, and read-only DPs can't be the target of any bindings, Fredrik Hedblad's PushBinding may be a possible workaround: OneWayToSource Binding for ReadOnly Dependency Property
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="pb:PushBindingManager.StylePushBindings">
<Setter.Value>
<pb:PushBindingCollection>
<pb:PushBinding TargetDependencyProperty="Controls:TreeViewHelper.IsMouseDirectlyOverItem"
Path="IsHovered" />
</pb:PushBindingCollection>
</Setter.Value>
</Setter>
<Style.Triggers>
...
</Style>
I have some frameworkElements inside XAML, and I define some properties like background, and cursor.
In code behind, I change these properties, and when an event triggers, I want to reload these initial properties defined in XAML. Is this possible or I need to redifine manually in code behind?
Thanks.
A control defined in XAML is essentially defining an instance. Once you have the instance, the object is just like every other object you deal with. Having access to the instance defined in XAML within your code behind is akin to creating a new object in the code behind and then adjusting its properties at run time.
When you want the property value to change; you don't revert your property changes, you simply change them to what you desire.
I would suggest looking into DataTriggers for making temporary changes based on some value. This will change the value of a property while a specific condition is true, and revert it to its original value when the condition is false.
For example, here's a style that will change the cursor to a Wait cursor while loading, and change the background to Red if it is invalid.
<Style TargetType="{x:Type local:MyUserControl}">
<Setter Property="Cursor" Value="Arrow" />
<Setter Property="Background" Value="White" />
<Style.Triggers>
<DataTrigger Binding="{Binding IsLoading}" Value="True">
<Setter Property="Cursor" Value="Wait" />
</DataTrigger>
<DataTrigger Binding="{Binding IsValid}" Value="True">
<Setter Property="Background" Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
Of course, you'll have to define the IsLoading and IsValid properties behind your UserControl, and set them to true/false at the appropriate times in your code-behind.
How would I change the background color of a TextBox Control in the Default Style Xaml to be a different color when the control is either Disabled or ReadOnly ?
You can achieve this with triggers in the style:
<TextBox>
<TextBox.Style>
<Style TargetType="TextBox">
<Style.Triggers>
<Trigger Property="IsReadOnly" Value="True">
<Setter Property="Background" Value="Green" />
</Trigger>
<Trigger Property="IsEnabled" Value="True">
<Setter Property="Background" Value="Red" />
</Trigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
Im not at a PC at the moment (just mobile) but I think you can edit the template of your control and there are some Visual States for your some controls that define things like disabled states, mouse overs, etc... which you should be able to redefine?
The way I accomplished this was to create a Converter for the control.
When the control is bound to an object it detects if the control is Enabled from this object that it is bound to. Based upon this it sets the background color for the Textbox accordingly.
Here's what I'm trying to do:
<Style x:Key="TreeViewItemStyle">
<Setter Property="TreeViewItem.ContextMenu" Value="{StaticResource ContextMenu}" />
<Style.Triggers>
<Trigger Property="TreeViewItem.ContextMenu.IsOpen" Value="True">
<Setter Property="TreeViewItem.BitmapEffect">
<Setter.Value>
<OuterGlowBitmapEffect GlowColor="Yellow" GlowSize="2"/>
</Setter.Value>
</Setter>
</Trigger>
</Style>
...
But it is obviously not working because Property="TreeViewItem.ContextMenu.IsOpen" is not recognized. Any suggestions to what I need to change?
You can bind to the IsOpened property of the context menu using a DataTrigger:
<DataTrigger Binding="{Binding ContextMenu.IsOpen, RelativeSource={RelativeSource Self}}" Value="True">
<Setter Property="Background" Value="Green"/>
</DataTrigger>
Unfortunately, since all of the items in TreeView share the same ContextMenu, that will highlight all of them at once. There doesn't seem to be a property that lets you find out which FrameworkElement opened the ContextMenu.
You could handle the ContextMenuOpening and ContextMenuClosing events on the TreeViewItem, since those will bubble up from the control that handled the click and pass through the right TreeViewItem. If you want to do it in XAML, you could use an EventTrigger to start a one-frame animation that changes your property. The cleanest option may be to write an attached behavior that handles the ContextMenuOpening and ContextMenuClosing events and sets an attached property to true when the context menu is open.
I have this in XAML in my style:
<DataTrigger Binding="{Binding Path=ButtonStyle}" Value="CutLeft">
<DataTrigger.Setters>
<Setter Property="CornerRadius" TargetName="border" Value="0 11 11 0" />
<Setter Property="CornerRadius" TargetName="border1" Value="0 10 10 0" />
</DataTrigger.Setters>
</DataTrigger>
And this XAML in my Window where i have the button:
<gui:MyCustomButton ButtonStyle="CutLeft"/>
And in MyCustomButton code:
public enum ButtonStyles {
CutLeft, CutRight, Circular, Normal
}
public partial class MyCustomButton
{
[DefaultValue(ButtonStyles.Normal)]
public ButtonStyles ButtonStyle { get; set; }
}
But it doesn't work! Any tips?
I think there are a couple of problems here.
The first is that you're not raising property change notifications for ButtonStyle. Either change ButtonStyle to a dependency property, or implement INotifyPropertyChanged on MyCustomButton, and raise the PropertyChanged event from the ButtonStyle setter. In this case, the dependency property approach is probably better, because at some point someone is going to want to set ButtonStyle through a Style, and only DPs can be styled.
The second is that your data trigger appears to be looking at the data context, which is probably not the control. (If you look in the Output window, you'll probably be seeing binding errors about not being able to find the 'ButtonStyle' property on some data object.) You can get around this by adding RelativeSource={RelativeSource Self} to your DataTrigger. However, if you change ButtonStyle to a DP as suggested, you should just be able to use a plain old Trigger, which automatically works against the properties of the control being styled:
<Style.Triggers>
<Trigger Property="ButtonStyle" Value="CutLeft">
<Setter Property="CornerRadius" TargetName="border" Value="0 11 11 0" />
</Trigger>
</Style.Triggers>
(Note also that you don't need to specify the Trigger.Setters or DataTrigger.Setters element. The XAML reader will fill that in for you.)