When my application change state to normal i want to set focus over my ListView:
private void Window_StateChanged(object sender, EventArgs e)
{
if (WindowState == WindowState.Normal)
ListViewUsers.Focus();
}
I am doind that in order to use Up & Down arrows Keys to navigate my ListViewItem instead of click on my ListView first to set focus.
So this works fine except the fact that i have this dotted border around my ListView.
I try to add this line to my ListViewItem Style:
<Style x:Key="ListBoxItemStyle1" TargetType="{x:Type ListViewItem}">
<Setter Property="FocusVisualStyle" Value="{x:Null}"/> ....
But unfortunately i still see this dotted border.
Any seggestions ?
It works fine for me.
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
How you apply style? Are you sure that ListView items use this style?
Related
I'm trying to change the color of each part of the scrollbar for ScrollViewer, ListView, and RichTextBox using XAML.
This works as a global override for all Scrollbar Backgrounds.
<Style TargetType="{x:Type ScrollBar}">
<Setter Property="Background" Value="#FF191919"/>
<Setter Property="BorderBrush" Value="#FF191919"/>
</Style>
But I need a style color for each part.
ScrollBar
Thumb
RepeatButton
Using Style TargetType doesn't work for any other part.
The Thumb and RepeatButtons stay default white/gray.
I've added a DataGrid to my WPF Window, and I've set the VerticalGridLinesBrush property in the XAML to show the vertical grid lines in the relevant colour. But I can't figure out how to increase width of the vertical grid lines, that are displayed in the DataGridRow.
Can someone please show me how to set the vertical grid line thickness in a WPF DataGrid?
set this properties
GridLinesVisibility="All" , VerticalGridLinesBrush="Red" and BorderThickness="1,1,5,0"
in dataGrid.
It seems there is no thickness setting for the GridLines... you can probably use DataGridCell properties instead.
<DataGrid GridLinesVisibility="Horizontal">
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="BorderThickness" Value="0,0,3,0"/>
<Setter Property="BorderBrush" Value="Red"/>
</Style>
</DataGrid.CellStyle>
I have a button in a Listview column. When I press the button, the row does not select. I found one answer to the problem by using:
<Style TargetType="ListBoxItem">
<Style.Triggers>
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter Property="IsSelected" Value="True"></Setter>
</Trigger>
</Style.Triggers>
</Style>
This works to select the row, but when the row loses focus it doesn't stay selected, and I need to be able to select multiple rows.
I also tried this solution:
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<EventSetter Event="PreviewGotKeyboardFocus" Handler="SelectCurrentItem"/>
</Style>
</ListView.ItemContainerStyle>
And in code behind:
private void SelectCurrentItem(object sender, KeyboardFocusChangedEventArgs e)
{
ListViewItem item = (ListViewItem) sender;
item.IsSelected = true;
}
This is closer to what I want, but now this affect selecting multiple items and now the user has to click twice on another item before it will select.
I also tried re-selecting the row in the click handler by using:
ListViewItem selectedRow = ((FrameworkElement)sender).DataContext as ListViewItem;
and
((sender as FrameworkElement).TemplatedParent as ListViewItem).IsSelected = true;
but these give me a null error. Any suggestions for what I'm doing wrong?
I have to use a Listview for other reasons, or I would just use a Datagrid, which works great.
Try adding a handler on the button click that reselects the row.
In my app (C# WPF) I have about 30 or 40 textBoxes in more grids and I want to change their foreground color in a loop. I use the code below and it works. But I want to use it for the whole project, not only for concrete grid
xaml code
<grid x:Name"stk">
.... some textBoxes ...
</grid>
*.cs code
foreach (TextBox item in this.stk.Children.OfType<TextBox>())
{
if (item.Name.StartsWith("txt"))
item.Foreground = Brushes.Orange;
}
So, when I have more grids, I have to put x:Name="..." into each one and this implies more foreach loops.
Much Simpler Way
Define a Style with TargetType set to Textbox and with no Key. This way this style will be applied to all textbox in the application without the need to bind the style or the foreground for each textbox.
<Application.Resources>
<SolidColorBrush Color="Red" x:Key="txtColor" />
<Style TargetType="TextBox">
<Setter Property="Foreground" Value="{DynamicResource txtColor}" />
</Style>
</Application.Resources>
To change the Foreground Color.
private void Button_Click(object sender, RoutedEventArgs e)
{
if (Application.Current.Resources.Contains("txtColor"))
{
Application.Current.Resources["txtColor"] = new SolidColorBrush(Colors.Blue);
}
}
Bind all your Textbox's Foreground to a common Brush Resource. Define the brush resource common to Project and access it everywhere.
In App.XML declare the brush resource so that you can access it anywhere from your project. [Note : You can also define it resource Dictionary and refer it]
<Application.Resources>
<SolidColorBrush Color="Red" x:Key="txtColor" />
</Application.Resources>
In All your textbox bind the foreground to the "txtColor" brush resource.
<TextBox Foreground="{DynamicResource txtColor}" Text="TextBox" />
To change the Foreground color of all textbox's, then change the commonly defined resource's color. Below I changed the color in button click. Access th resource using the key and set the new brush which you want to set.
private void Button_Click(object sender, RoutedEventArgs e)
{
if (Application.Current.Resources.Contains("txtColor"))
{
Application.Current.Resources["txtColor"] = new SolidColorBrush(Colors.Blue);
}
}
Ignore my code and have a look at this answer
Find all controls in WPF Window by type
So ... To solve my problem where I couldn't change foreground color of textBoxes when some textBox is disabled ... I used code below ...
<Application.Resources>
<Style TargetType="TextBox">
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="Orange"/>
</Trigger>
<Trigger Property="IsEnabled" Value="True">
<Setter Property="Foreground" Value="Green"/>
</Trigger>
</Style.Triggers>
</Style>
</Application.Resources>
What about creating a "usercontrol" based on the standard textbox where you control the appearance of the foreground. This way you have a reusable control that you can use anywhere you want and have "full control" over it's appearance and behaviour. Take a look at this article, or this one for some examples that might help you go the right way ;)
Is it possible to have a TextBox that is disabled by default, but becomes enabled when a user double-clicks it?
you can place your TextBox inside StackPanel like this:
<StackPanel MouseLeftButtonDown="StackPanel_MouseDown">
<TextBox Name="textBox1"/>
</StackPanel>
Then in StackPanel event handler:
private void StackPanel_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ClickCount >= 2)
{
textBox1.IsEnabled = true; //only hit here on DoubleClick
}
}
you can also simulate StackPanel DoubleClick as described on this question:
WPF StackPanel with Click AND DoubleClick
That's very unusual, also when a control is disabled it is not expected to get input. Users seeing a disabled control normally would not even try to click/double click on it.
Maybe you can add a check box to enable it (or the function belonging to it), or show a message box when double clicking it when it is not allowed/meant to. In this case you also can clearly add a reason why it cannot be double clicked.
What I have seen before is a checkbox without text right before the control. When you click the checkbox it enables the control (text box in your case) after it. You can even use a tooltip for the check box to provide help information what the checkbox is doing.
I would try attaching to the PreviewMouseDown event and enable/disable there.
Otherwise you will have to do the old VB6 trick of having a transparent control above the textbox to receive the click event.
This question is old, but maybe I can help someone that finds a solution for this.
In a recent project I've needed to simulate two states: view and edit. I've do this using a textbox. In view state, value is displayed but you cannot got focus clicking on the control. To enable editing mode you need to double click the control. To avoid the control getting focus by clicking on it and also the disadvantages of disable it, I've used two preview events for control the behaviour of the textbox and adapt it's responses to application needs and state. One of the events is PreviewMouseDown:
private void tbxVariable_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
if(!tbxVariable.IsFocused && e.ChangedButton == MouseButton.Left)
e.Handled = true;
}
In this event we will block the mouse down button if our textbox isn't focused yet. This prevents textbox to get focus. So it will behave like a label. When the control is focused, this event isn't blocked and is propagated towards the control. Note that maybe you need to change the cursor because editing cursor is used when the mouse is over the control. Note also, that we are blocking only the left button.
The second event looks like:
private void tbxVariable_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left && !tbxVariable.IsFocused)
tbxVariable.Focus();
}
In the second event we will bring the focus to the control on double left click if it isn't focused yet. If control has focus so we will let the event propagate and the control will behave normally.
In my case I've created a special style for the textbox leaving borders, backgrounds, and all style behaviours. This is the XAML code:
<Style x:Key="InlineEditorTextBox" TargetType="{x:Type TextBox}">
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="OverridesDefaultStyle" Value="True" />
<Setter Property="KeyboardNavigation.TabNavigation" Value="None" />
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
<Setter Property="MinWidth" Value="5" />
<Setter Property="MaxHeight" Value="16" />
<Setter Property="AllowDrop" Value="false" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBoxBase}">
<ScrollViewer Margin="0" x:Name="PART_ContentHost" VerticalScrollBarVisibility="Disabled" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>