I want to override the width of the scroll bar of ScrollViewer in WPF. I manage to widen its width but when I tried to make it slimmer it didn't work. It seems that it has a minimum width that can't be overridden. Below is the code that I have.
Why is the code below does not work to make the width of the scrollbar slimmer?
Thanks
<ScrollViewer.Resources>
<Style TargetType="ScrollBar">
<Style.Triggers>
<Trigger Property="Orientation" Value="Vertical">
<Setter Property="Width" Value="20"/>
</Trigger>
</Style.Triggers>
</Style>
</ScrollViewer.Resources>
You need to edit the template.
Place the editor cursor inside the ScrollViewer tag and then in your properties window go to "Miscellaneous -> Template", click the little rectangle to the right and select "Convert to New Resource" from the popup menu:
That will create a new template containing, among other things, a scrollbar named PART_VerticalScrollBar. Put the cursor over that scrollbar tag and repeat the above to template that out as well, this will create a control template that starts like this:
<ControlTemplate x:Key="ScrollBarControlTemplate1" TargetType="{x:Type ScrollBar}">
<Grid x:Name="Bg" SnapsToDevicePixels="True">
<Grid.RowDefinitions>
<RowDefinition MaxHeight="{DynamicResource {x:Static SystemParameters.VerticalScrollBarButtonHeightKey}}"/>
Now set MaxWidth=10 (or whatever on that Grid element).
The only other problem you may encounter is that if you make it too small then the up/down arrows on the scrollbar buttons might disappear, that's because their default template places a margin around their path:
<Path x:Name="ArrowTop" Data="M0,4C0,4 ..etc ... " Fill="#FF606060" Margin="3,4,3,3" Stretch="Uniform"/>
This can be easily solved by removing that margin setting and/or replacing the path altogether with something else.
Related
I'm trying to style this button, and I even though I state the height it should be in my XAML, the template in the styling seems to get rid of it.
Note: I am aware I can just have a style with no template, but I need the template because I'm using multiple themes / style files.
Control:
<Button Style="{DynamicResource basicButton}"
Height="50">
<Canvas...>
<Path... />
</Canvas>
</Button>
Styling:
<Style x:Key="basicButton" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="{DynamicResource solid_single_mainColor}">
<ContentPresenter />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Is this expected behavior? I intent to re-use this button style, so I need to be able to give it a height in the XAML. What am I missing? How can I give each new button I create a different height?
The problem is likely coming from the Canvas that you are placing inside the Button. In many cases a Canvas will have unbounded size unless you explicitly clip it. The Height of the Button itself isn't affected by its template as layout measurement will use the explicit value you have set but you may not be able to tell visually what size the Button itself is.
Suppose I have n number of rectangles.
I want them to behave such that when I click on any rectangle it should be filled white and all other should be filled with black color.
I know that I can create a style. But while creating style I can tell the trigger that fill the rectangle with black color but how can I say that fill all the remaining rectangles with white color.
Can anyone suggest me?
I want just a small hint.
Don't make rectangles work like RadioButtons, make RadioButtons look like rectangles.I would suggest to use RadioButton instead and change template to make it look like rectangle. Then you can trigger change of background based on RadioButton.IsChecked property. This way RadioButton will take care of tick/untick operation and trigger background change in other RadioButtons in the same group.
<Style TargetType="{x:Type RadioButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type RadioButton}">
<Border BorderBrush="Black" BorderThickness="1" Background="Black" x:Name="PART_Border">
<ContentPresenter/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter TargetName="PART_Border" Property="Background" Value="White"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
and use your RadioButton like this:
<RadioButton>
<RadioButton.Content>
<!-- content to be displayer inside border -->
</RadioButton.Content>
</RadioButton>
You've answered the question yourself. Take two radio buttons and try editing the template. Basically when you'll have the close look at the template you will find an ellipse with a dot whose visibility gets toggled off on mouse click
Instead of having the ellipse you can have a rectangle/border with some thickness and set its background color on mouse click
The WPF Grid has an "IsMouseOver" property that you can use in the Grid's Style's Triggers.
My problem is that the "IsMouseOver" property only changes if the mouse is over some control (ie a Button, or ComboBox) within the Grid itself.
For example:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="25" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Button Grid.Column="1">A Button</Button>
<Grid.Style>
<Style TargetType="{x:Type Grid}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="False">
<Setter Property="Opacity" Value="0.5"></Setter>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Opacity" Value="1"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</Grid.Style>
</Grid>
The above Grid and it's contents will be displayed in half opacity so that you can see the controls.
You will notice that the opacity will not be set to full if you hover over the first column (which doesn't contain anything).
However the opacity will be set to full if you hover over the button in the second column.
In my application, the Grid that I'm setting the triggers for is being displayed on top of an image control. I do not want the Grid to be displayed until the mouse is hovering over the image... In other words, since the Grid is on top of the image, I don't want the grid to be displayed until the mouse is hovering over the Grid (anywhere in the grid) because the Grid is on top of the image.
Does anyone know how to accomplish this?
Thanks!
-Frinny
Your problem is that the Grid itself is not hit-testable because it has no background. Try this instead:
<Grid Background="Transparent">
set the grids background to transparent, then it should work
for details why this is so please look here
I'm trying to style a Silverlight DataGrid so that the content displayed in the grid wraps. I want each grid row (and the grid header), to consist of TWO lines of cells, instead of one. This makes each row taller, but keeps all of the content visible on screen simultaneously, instead of having to scroll horizontally to see all of the fields. Here is a picture to help illustrate what I am going for:
Screenshot
(I don't have enough rep to post an image directly but the link above will show you an example screen shot)
I see the templates that let me customize how the different cells are styled, but I'm not seeing anything that will let me to control how those cells are displayed beside each other on the screen.
Your best bet here is going to be to abandon the datagrid and use a ListView instead, and inside the ListView you will want to show a UserControl of your own design. I did something similar for an application I built.
In your XAML for your ListView you will want to set the ItemContainerStyle and inside that you'll want to display your custom UserControl (in which you can use a Grid to setup the rows/colums and their spans). It basically looks like this:
<ListView
Name="_listView"
Grid.Row="0" Grid.Column="0"
SelectionMode="Single"
IsSynchronizedWithCurrentItem="True"
SelectedItem="{Binding SelectedAgent, Mode=TwoWay}"
ItemsSource="{Binding Agents, Mode=OneWay}"
GridViewColumnHeader.Click="ListView_Click"
DependencyProperties:ListBoxClickCommand.ClickCommand="{Binding ShowAgentDetailsCommand}">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<Border
Name="_border"
Padding="2"
CornerRadius="5"
SnapsToDevicePixels="true"
Background="Transparent">
<Controls:AgentStateControl></Controls:AgentStateControl>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="_border" Property="Background" Value="CornflowerBlue" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListView.ItemContainerStyle>
The AgentStateControl is my custom control, and it has two rows, the second has a bigger span on the columns. You can create that control however you want.
There is some way, a property like VerticalContentAlignment for aligning grid children or do I have to do it manually one by one?
If most of the grid's children are of the same type you can use styles, for example putting this at the start of the grid will align all text boxes
<Grid>
<Grid.Resources>
<Style TargetType="TextBox">
<Setter Property="HorizontalAlignment" Value="Right"/>
</Style>
</Grid.Resources>
.
.
.
If you have only a few types of controls inside the grid this is very useful.
If TextAlignment works for you instead of HorizontalAlignment, it can be made even simpler:
<Grid TextBlock.TextAlignment="Right">
<TextBox ....
....