Clear binding using XAML - wpf

I'm creating a wpf application, which holds a text box.
When the text box is disabled (the default state) I'm binding it to something, but when the user double click the text box it's changing its state to enabled and the user can edit it.
But when I type something after the text box is enabled, it's deleted every second because of the trigger (maybe the ui is updating and the trigger with him). When I did using code and just cleared the binding after double click it worked fine. But I don't want to do it through code, I know that I'm just missing a little thing for the XAML to work.
This is my current XAML:
<Style.Triggers>
<Trigger Property="TextBox.IsEnabled" Value="False">
<Setter Property="Text" Value="{Binding some binding}"/>
</Trigger>
<Trigger Property="TextBox.IsEnabled" Value="True">
<Setter Property="Text" Value="{x:Null}"/>
</Trigger>
</Style.Triggers>
Thanks!

Would it help to set the Binding but change its direction to OneWayToSource?
<Style.Triggers>
<Trigger Property="TextBox.IsEnabled" Value="False">
<Setter Property="Text" Value="{Binding someBinding}"/>
</Trigger>
<Trigger Property="TextBox.IsEnabled" Value="True">
<Setter Property="Text" Value="{Binding Path=someBinding, Mode=OneWayToSource}"/>
</Trigger>
</Style.Triggers>

Related

Disable focus on header - listview wpf

I'm using wpf and I've listview.
Everything just works fine, but I want to disable focusing on header (when i enter header with mouse it changes color to blue).
I mean this
This will do the work.
<GridView.ColumnHeaderContainerStyle>
<Style TargetType="{x:Type GridViewColumnHeader}">
<Setter Property="IsEnabled" Value="False"/>
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="TextElement.Foreground" Value="Black"/>
</Trigger>
</Style.Triggers>
</Style>
</GridView.ColumnHeaderContainerStyle>

WPF DataGrid RowStyle for selected row not changing the background and foreground color

I am using Visual Studio 2012 on windows 7. I need to know why the following style for Grid's selected row does not work for background and foreground colors but works perfectly fine for other properties like BorderBrush and BorderThickness etc? Though I can see them changing while mouse over grid rows.
<Style x:Key="gridRowStyle" TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="PeachPuff"/>
<Setter Property="Foreground" Value="BlueViolet"/>
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="PeachPuff"/>
<Setter Property="Foreground" Value="BlueViolet"/>
<Setter Property="BorderBrush" Value="BlueViolet" />
<Setter Property="BorderThickness" Value="2" />
</Trigger>
</Style.Triggers>
</Style>
Here is how I am using on grid.
<DataGrid RowStyle="{StaticResource gridRowStyle}">
I am stressing on to know "why" rather than solution to the problem as I already have solution to problem if I use grid cell style instead of rowstyle like following:
<Style x:Key="gridCellStyle" TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="PeachPuff"/>
<Setter Property="Foreground" Value="BlueViolet"/>
</Trigger>
</Style.Triggers>
</Style>
In the Default style of the DataGridCell having the following default style trigger.
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
<Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
</Trigger>
So if you written trigger for DataGridRow, then it will only apply to the element that was placed before the DataGridCell in visual tree.
So to change the Background & Foreground while selection, you must write the trigger in DataGridCell style or remove the default trigger from the style.
Simply remove this attributs at row level in the datagrid, they have priority over the trigged properties.
nezac

wpf DataGrid Loses its Row Selection when the focus is lost

I have come accross a very strange behavior with the DataGrid. I have following Trigger on the DataGridRow
<Style TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="{StaticResource SelectionBackgroundBrush}"/>
<Setter Property="Foreground" Value="White"/>
</Trigger>
</Style.Triggers>
</Style>
Initially when the row is selected, I get the behavior from the above trigger. However, after selection, if the DataGrid loses focus (say for example I click some other button on window) the Foreground property loses its value, but the background remains as specified in the trigger. Has anyone ever come accross this behavior, or there is some problem with my code above (or elsewhere in my applicaion for that matter). Any workarounds for the above issue ?
I've used DataGridCell instead of DataGridRow and it works for me
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<Trigger Property="DataGridCell.IsSelected" Value="True">
<Setter Property="BorderBrush" Value="#CCDAFF" />
<Setter Property="Background" Value="#3399ff"/>
<Setter Property="Foreground" Value="White"/>
</Trigger>
</Style.Triggers>
</Style>
Hope it helps someone!

Style of Controle At various Event

Could anyone tell what would be the style of the Label or Button at various event shows in the below image.
1. MouseOver
2 MousePressed and Control Selected
You can have a look at Style triggers. To figure out what properties you need you can have a look at the UIElement.
For example it can be something like:
<Style x:Key="Triggers" TargetType="Button">
<Style.Triggers>
<Trigger Property="IsPressed" Value="true">
<Setter Property = "Background" Value="LightGray"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property = "BorderBrush" Value="Blue"/>
</Trigger>
<Trigger Property="IsFocused" Value="true">
<Setter Property = "BorderBrush" Value="LightGray"/>
</Trigger>
</Style.Triggers>
</Style>
The above style does not recreate the look of the button from the image, it's just a sample on how it can be done.
You can have a look at the Button class to see what properties you can change.
If I understood your question properly, I think you can change the opacity on MousePressed and Mouseover events.

How to set the border for a WPF DataGrid Row which is currently having the focus

I want to set the border for a DataGrid Row which is currently having the focus. But not the seleced row because when the Multi selection is enabled for the datagrid then there is a chance that multiple rows can be selected.
I need a solution in XAML
Thanks in advance!
Add this to DataGridRow's style (either using Resources, or by setting DataGrid.RowStyle):
<Style TargetType="DataGridRow">
<Style.Triggers>
<Trigger Property="IsFocused" Value="true">
<Setter Property="BorderBrush" Value="Red" />
<Setter Property="BorderThickness" Value="1" />
</Trigger>
</Style.Triggers>
</Style>
Try IsKeyboardFocusWithin Property
<Style.Triggers>
<Trigger Property="IsKeyboardFocusWithin"
Value="True">
<Setter Property="BorderBrush"
Value="Red" />
</Trigger>
</Style.Triggers>

Resources