disable row selection in winui3 datagrid - c#

I have been trying very hard to disable hover, mouse over and row selections on datagrid rows. I had used the following code for Wpf application in the past and it worked perfectly. However, I am in the middle of the process of migrating my code to the new Winui3 and I just can't make it work again.
but the problem is how to hide row selections. See this picture:
Here is the code that works for Wpf;
<controls:DataGrid.Style>
<Style TargetType="controls:DataGridCell">
<Setter Property="BorderBrush" Value="Transparent" />
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
</Style>
</controls:DataGrid.Style>
Now, FocusVisualStyle doesn't exists. I was able to disable cell borders on selection by overriding some brush resources like these:
<SolidColorBrush x:Key="DataGridCellFocusVisualPrimaryBrush" Color="Transparent" />
<SolidColorBrush x:Key="DataGridCellFocusVisualSecondaryBrush" Color="Transparent" />

This should work:
<controls:DataGrid>
<controls:DataGrid.Resources>
<Color x:Key="DataGridRowSelectedBackgroundColor">Transparent</Color>
<Color x:Key="DataGridRowSelectedHoveredUnfocusedBackgroundColor">Transparent</Color>
<Color x:Key="DataGridRowSelectedUnfocusedBackgroundColor">Transparent</Color>
<!--
This one is better not being just "Transparent".
This way you won't lose visual effects for hovered selected rows.
-->
<StaticResource
x:Key="DataGridRowSelectedHoveredBackgroundColor"
ResourceKey="SystemListLowColor" />
</controls:DataGrid.Resources>
</controls:DataGrid>
You can find the colors in the GitHub repo.

Related

Override style on parts of ComboBox scrollbar

After lots of research, I stumbled across a relatively simple way to target just specific parts of the control style without using the entire control template. It's partially successful, but I need a little help getting all the way to the end.
Specifically, I am trying to override the Thumb button color of the scrollbar in the dropdown of a ComboBox. The cool technique I came across is the following, which utilizes nested Style.Resources to access the lower objects...
<Style x:Key="MyComboBoxStyle" TargetType="{x:Type ComboBox}">
<Style.Resources>
<Style TargetType="ScrollViewer">
<Style.Resources>
<Style TargetType="ScrollBar">
<Setter Property="Background" Value="LightGreen" />
<Style.Resources>
<Style TargetType="Track">
<Setter Property="Cursor" Value="Cross" />
<Style.Resources>
<Style TargetType="Thumb">
<Setter Property="Background" Value="Red"/>
<Setter Property="Cursor" Value="Hand" />
</Style>
</Style.Resources>
</Style>
<Style TargetType="RepeatButton">
<Setter Property="Background" Value="Red"/>
<Setter Property="Cursor" Value="Hand" />
</Style>
</Style.Resources>
</Style>
</Style.Resources>
</Style>
</Style.Resources>
<!--- rest of ComboBox style definition follows... -->
By sequentially drilling down into the complex control style tree, using nested Style.Resources, I am able to target specific aspects of a control style, without needing the entire style definition.
The xaml code above successfully drills all the way down to the "Track"... I can change things like the margin, cursor, etc. of the Track. But, I just can't seem to get that last step to the Thumb. Also, I can't seem to access the Repeat buttons, which should be at the same level (in the object tree) as the Track.
Looking at the style template for ScrollBars seems to show that the object tree is ScrollBar->Track->Thumb... but I seem to be missing something?
Any ideas on how to get access to the Thumb color?
The default style for the ScrollBar sets the Style property of the Thumb explicitly in the Track:
<Track x:Name="PART_Track" ...>
...
<Track.Thumb>
<Thumb Style="{StaticResource ScrollBarThumbVertical}"/>
</Track.Thumb>
</Track>
This means that your implicit Thumb style won't be applied.
So you will have to define a custom complete ControlTemplate for the ScrollBar to be able to modify the style/template of the Thumb.
Alternatively, you may consider to look it up in the visual tree at runtime and set any of its properties programmatically.

How do I change the accent colour of a Xamarin.Forms UWP application?

I am developing a Xamarin.Forms UWP application.
I am struggling to set the accent colour of my application. This is the colour that is used for certain behaviors by default on controls.
For example the Entry control has a default blue highlighting on focus shown below:
I have tried a few suggestions from this thread: Change Accent Color in Windows 10 UWP but none seemed to work.
I am not sure whether it is because I didn't fully understand how changing the colour for UWP differs for Xamarin.UWP, or whether what I'm trying to do is even possible with Xamarin.Forms.
Has anyone found out how to do this?
Here is the style code of FormsTextBox for UWP.
You need to override below styled colors:
<Setter Property="Foreground" Value="{ThemeResource SystemControlForegroundBaseHighBrush}" />
<Setter Property="Background" Value="{ThemeResource SystemControlBackgroundAltHighBrush}" />
<Setter Property="BackgroundFocusBrush" Value="{ThemeResource SystemControlBackgroundChromeWhiteBrush}" />
<Setter Property="BorderBrush" Value="{ThemeResource SystemControlForegroundChromeDisabledLowBrush}" />
So to change the colour of your textbox boarder brush you can add these ThemeResources to your App.xaml like so:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Light">
<SolidColorBrush x:Key="SystemControlHighlightAccentBrush" Color="#ff0000" />
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>
</ResourceDictionary>
</Application.Resources>
You can define a style setter property in App.xaml
<ResourceDictionary>
<Style TargetType="Button" x:Name="myNewButtonStyle">
<Setter Property="Background" Value="{ThemeResource ButtonBackgroundThemeBrush}" />
</Style>
</ResourceDictionary>
Then use CustomRenderer for the controls you need to change colors
protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
{
base.OnElementChanged(e);
if (this.Element != null)
{
this.Control.Style = Windows.UI.Xaml.Application.Current.Resources["myNewButtonStyle"] as Windows.UI.Xaml.Style;
}
}
in a similar way you would be able to use Themed Resource Dictionary key and apply. This code can be used to have native styles on Xamarin's control.
The absolute easiest way is to go to the styles.xml file which is located in Resources/values and uncomment:
<item name="colorAccent">#FF2081</item>
This changes the accent color on the whole project eg with Entrys.

How can I change the default style of WPF controls for a single assembly?

I've written a WPF plugin for some off-the-shelf product, and in this plugin I've used a theme/style to change the minimal width of all buttons like so:
<Style TargetType="Button">
<Setter Property="MinWidth" Value="80" />
</Style>
In the newest version of said off-the-shelf product they migrated from winforms to WPF themselves. Now when my plugin is loaded the style that previously just affected my plugged-in forms now affects all buttons in the application. This renders most UI's unusable.
I know I can use dictionary key based resources to make this style specific to my buttons, but this means I have to change each and every button in my plugin by hand, as well as not forget to set the style of each button in the future (and other elements this problem applies to). There are other options to make the style specific to a set of buttons, as seen in Is it possible to set a style in XAML that selectively affects controls? But I'm looking for a way to let my style affect only those of my plugin (so a bit more coarse than talked about in the referenced question). This plugin consists of multiple windows/views (tied together with Caliburn.Micro).
Is there a way to easily scope a style to for instance an assembly or namespace? I'd really like to define my resources just once. Currently it's defined at the Application.Resources level, if there's one more appropriate I'd like to hear that too.
With a ResourceDictionary, we can set default style wich will be applied without define style in Xaml.
DictionayNew.xaml :
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Media="clr-namespace:System.Windows.Media;assembly=PresentationCore"
xmlns:System="clr-namespace:System;assembly=mscorlib">
<!-- default button -->
<Style TargetType="{x:Type Button}">
<Setter Property="FontWeight" Value="Normal" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="MinWidth" Value="80" />
</Style>
<!-- new button style -->
<Style x:Key="ActionButton" TargetType="{x:Type Button}">
<Setter Property="FontWeight" Value="Normal" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="MinWidth" Value="75" />
<Setter Property="Height" Value="23" />
</Style>
<!-- new button style based on previous style -->
<Style x:Key="BigActionButton"
BasedOn="{StaticResource ActionButton}"
TargetType="{x:Type Button}">
<Setter Property="MinWidth" Value="150" />
<Setter Property="Height" Value="30" />
</Style>
</ResourceDictionary>
In your Xaml, use the dictionary :
<Window x:Class="CheckDoublonImageBing.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="DictionaryNew.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
</Grid>
</Window>
Then, use Button as usual or with new style
<Button Content="Refresh" />
<Button Content="Delete selected" Style="{DynamicResource ActionButton}" />
With no style defined, button will have default style defined in the dictionary.
EDIT :
You can set merged dictionary by code like this :
ResourceDictionary myResourceDictionary = new ResourceDictionary();
myResourceDictionary.Source = new Uri("DictionayNew.xaml", UriKind.Relative);
Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
You need to specify a Key for your Style and apply the Style to all your Buttons.
<Style TargetType="Button" x:Key="MyButtonStyle">
<Setter Property="MinWidth" Value="80" />
</Style>
<Button Style="{StaticResource MyButtonStyle}"/>
Without a Key the Style is used for all Buttons in the application (as you have already noticed).

Change foreground color of ComboBoxItem when ComboBox is hovered

At the moment, I'm styling WPF controls. This is how my custom <Button> looks hovered.
And this is how <ComboBox> looks when mouse is over it.
Simply, I want to be able to change foreground color of currently selected ComboBoxItem when whole ComboBox is hovered. In this example, I'd like to have similiar yellow color like on <Button> control.
The problem is, that <ComboBox> has different ControlTemplates for ComboBox item and toggle button. I don't know how to interact between those two.
The template for <ComboBox> is pretty big, so I'm not going to post it here. Also, you don't have to post full answer if it's not required to explain the problem.
OK, I found a solution. You need to edit default ComboBox templates.
In Visual Studio 2013 you can get a default template in Designer by clicking right mouse button on ComboBox -> Edit template -> Edit a copy...
In <ControlTemplate x:Key="ComboBoxTemplate" TargetType="{x:Type ComboBox}"> you have to add a following trigger:
<ControlTemplate.Triggers>
...
<Trigger Property="IsMouseOver" TargetName="toggleButton" Value="true">
<Setter Property="TextElement.Foreground" TargetName="contentPresenter" Value="Yellow" />
</Trigger>
...
</ControlTemplate.Triggers>
Also, apply a style to ComboBoxItem:
<Style TargetType="{x:Type ComboBoxItem}">
<Setter Property="Foreground" Value="Black"/>
</Style>
Now, it works like a charm.

How to change the default background in listbox when it's pressed?

I am using a few listboxes to display a data that I get from a webservice. Also, I custimized the way the data is displayed. All is working OK.
The only problem I'm having is when I clicked at any ListBoxItem and them don't cover the entire hight of the listbox, the remaining space change its background to a default color like: WhiteSmoke or something like that.
I'm using the ItemDataTemplate to display the customize data and the ItemContainerStyle to change the way the listbox interact in any state like: pressed, selected, unfocus, etc.
Does anyone have an idea how to change this?
Regards!
I'm guessing you've got the situation where your list items don't stretch all the way across the ListBox and so you're seeing the highlight underneath them, you can wipe out those colours with
<ListBox.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
</ListBox.Resources>
if you want to override the default background of the listbox write in the xaml of your WPF:
<Style TargetType="{x:Type ListBox}" >
<Setter Property="Background" Value="thecoloryouwant" />
</Style>
If I understand you correctly, you want to set the Background property of the ListBoxItem... you can do this like this:
<ListBox ItemsSource="{Binding SomeCollection}">
<ListBox.ItemContainerStyle>
<Style>
<Setter Property="Control.Background" Value="WhiteSmoke" />
</Style>
</ListBox.ItemContainerStyle>
</ListBox>

Categories