How to force a usercontrol width to be equal designer width? - wpf

I have a custom textbox in WPF and I want to change its width while resizing it in designer view. I used Binding ActualHeight but it doesn't work.
As the picture shows I resized the TextBox in design mode but it is still in the same width, not changed. How can I change that control's Width(only) property according to resizing?
DottedTextBox
<UserControl x:Class="Easy_Message.DottedTextBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="50" d:DesignWidth="400">
<Viewbox>
<TextBox x:Name="roottxt" Background="White"
BorderThickness="0" Width="{Binding ActualWidth,RelativeSource={RelativeSource TemplatedParent}}" Height="20">
<TextBox.Template>
<ControlTemplate TargetType="TextBox">
<Grid>
<Border x:Name="b" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}"
BorderThickness="1,0,1,0" BorderBrush="#c2c4cb">
<Border BorderThickness="0,1,0,0" BorderBrush="#9da0aa">
<Border BorderThickness="0,0,0,1" BorderBrush="#d9dae0">
<DockPanel>
<TextBox x:Name="orgbox" VerticalContentAlignment="Center"
HorizontalAlignment="Left"
BorderThickness="0"
FontSize="{TemplateBinding FontSize}"
Background="White"
Width="{Binding Path=Width,ElementName=b,Converter={StaticResource wconv}}" SelectionBrush="{TemplateBinding SelectionBrush}"
Height="20">
</TextBox>
<Border x:Name="brdDot" Background="{TemplateBinding Background}" Grid.Column="1"
Width="16" Height="{TemplateBinding Height}" HorizontalAlignment="Right"
BorderThickness="0">
<Canvas HorizontalAlignment="Right"
Width="10" Height="6"
Margin="0,0,2.5,2.5">
<Rectangle Width="1.5" Height="1.5" Fill="Black">
<Rectangle.RenderTransform>
<TranslateTransform X="0" Y="2"/>
</Rectangle.RenderTransform>
</Rectangle>
<Rectangle Width="1.5" Height="1.5" Fill="Black">
<Rectangle.RenderTransform>
<TranslateTransform X="4" Y="2"/>
</Rectangle.RenderTransform>
</Rectangle>
<Rectangle Width="1.5" Height="1.5" Fill="Black">
<Rectangle.RenderTransform>
<TranslateTransform X="8" Y="2"/>
</Rectangle.RenderTransform>
</Rectangle>
</Canvas>
</Border>
</DockPanel>
</Border>
</Border>
</Border>
</Grid>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding Path=IsMouseOver, ElementName=brdDot}" Value="True">
<Setter TargetName="brdDot" Property="Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0.5" EndPoint="1,0.5">
<GradientStop Color="White" Offset="-0.1"/>
<GradientStop Color="#ccdfff" Offset="0.6" />
<GradientStop Color="White" Offset="1.1" />
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter TargetName="brdDot" Property="BorderThickness" Value="1,0,0,0" />
<Setter TargetName="brdDot" Property="BorderBrush" Value="#ccc9c9"/>
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</TextBox.Template>
</TextBox>
</Viewbox>

Related

WPF Indeterminate Rounded Edge, Indicator overlap parent

Good Evening Guys, I am new to WPF application. I have a trouble with Indeterminate progress bar design. I had researched a lot of progress bar related topic via online but I still not clear about the progress bar styling concept.
I had tried on changing the value of corner radius and set true for clip to bounds on every possible element that affected the corner radius (PART_Indicator,PART_Track,Indicator,Animation), but still no luck with this.
The Child Border always overlap it's parent. Shown as below
What I want to achieve on Progress bar shown as below.
Below is my code:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="150">
<Grid>
<StackPanel Orientation="Vertical">
<ProgressBar Height="36" Name="progBar" VerticalAlignment="Top" IsIndeterminate="True" Foreground="Orange" BorderBrush="Gray" BorderThickness="1" >
<ProgressBar.Style>
<Style TargetType="{x:Type ProgressBar}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ProgressBar" >
<Grid Name="TemplateRoot" SnapsToDevicePixels="True">
<Rectangle RadiusX="2" RadiusY="2" Fill="Transparent" />
<Border CornerRadius="10" Margin="1,1,1,1">
<Border.Background>
<SolidColorBrush Color="Transparent"/>
</Border.Background>
</Border>
<Border BorderThickness="1" BorderBrush="gray" Margin="1,1,1,1" CornerRadius="10">
<Border.Background>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="White" Offset="0.0" />
<GradientStop Color="WhiteSmoke" Offset="1" />
</LinearGradientBrush>
</Border.Background>
</Border>
<Rectangle Name="PART_Track" Margin="1,1,1,1" ClipToBounds="True"/>
<Decorator Name="PART_Indicator" Margin="3,2,3,2" HorizontalAlignment="Left" ClipToBounds="True">
<Grid Name="Foreground" ClipToBounds="True">
<Rectangle RadiusX="10" RadiusY="10" Name="Indicator" ClipToBounds="True"/>
<Grid Name="Animation" ClipToBounds="True">
<Border Name="PART_GlowRect" Width="50" Margin="1" HorizontalAlignment="Left" Background="Orange" CornerRadius="10" />
</Grid>
<Grid Name="Overlay">
</Grid>
</Grid>
</Decorator>
<Border BorderThickness="0" CornerRadius="0" BorderBrush="Transparent" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ProgressBar.Style>
</ProgressBar>
</StackPanel>
</Grid>
</Window>
What had I did wrong on the code? Please for guide me regarding this issue.
Thank you.
Check out Clip property:
<Image
Source="sampleImages\Waterlilies.jpg"
Width="200" Height="150" HorizontalAlignment="Left">
<Image.Clip>
<EllipseGeometry
RadiusX="100"
RadiusY="75"
Center="100,75"/>
</Image.Clip>
</Image>
You can put any geometry inside Clip property. In your case it can be RectangleGeometry with RadiusX and RadiusY properties set.
More info: https://msdn.microsoft.com/ru-ru/library/system.windows.uielement.clip(v=vs.110).aspx
Thanks for Mikolaytis's answer, I am currently using RectangleGeometry.
I believe the Rect in RectangleGeometry it will not inherit the width from the parent width, I think I need to use code behind to recalculate the rect for different resolution.
I am sharing my code below in case somone facing the same issue as I was.
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="400">
<ProgressBar Height="36" Width="358" Name="progBar" VerticalAlignment="Top" IsIndeterminate="True" Foreground="Orange" BorderBrush="Gray" BorderThickness="1" >
<ProgressBar.Style>
<Style TargetType="{x:Type ProgressBar}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ProgressBar" >
<Grid Name="TemplateRoot" SnapsToDevicePixels="True">
<Rectangle RadiusX="2" RadiusY="2" Fill="Transparent" />
<Border CornerRadius="10" Margin="1,1,1,1">
<Border.Background>
<SolidColorBrush Color="Transparent"/>
</Border.Background>
</Border>
<Border BorderThickness="1" BorderBrush="Blue" Margin="1,1,1,1" CornerRadius="10">
<Border.Background>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="Red" Offset="0.0" />
<GradientStop Color="Red" Offset="1" />
</LinearGradientBrush>
</Border.Background>
</Border>
<Rectangle Name="PART_Track" Margin="0" ClipToBounds="True" />
<Border Background="black" Name="PART_Indicator" Margin="0" HorizontalAlignment="Left" ClipToBounds="True">
<Border.Clip>
<RectangleGeometry Rect="2,2,354,32" RadiusX="9" RadiusY="9.5" />
</Border.Clip>
<Grid Name="Foreground">
<Rectangle RadiusX="10" RadiusY="10" Name="Indicator" ClipToBounds="True"/>
<Grid Name="Animation" ClipToBounds="True">
<Border Name="PART_GlowRect" Width="50" Margin="1" HorizontalAlignment="Left" Background="Orange" CornerRadius="10" />
</Grid>
<Grid Name="Overlay">
</Grid>
</Grid>
</Border>
<Border BorderThickness="0" CornerRadius="0" BorderBrush="Transparent" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ProgressBar.Style>
</ProgressBar>
</Window>

WPF - Custom Tab Control Binding

I'm creating a custom tab control and the tabs were working fine, but when I click a button in my toolbar, the selected tab disappears.
How do I get the selected tab to remain Visible until the next tab is selected?
How do I bind the Visibility of the selected tab with the Visibility of it's corresponding toolbar and DataGrid? (Example: If I click on Tab2, I want Tab2Tools and dgTab2 to become Visible, and all other TabTools and DataGrids to be Hidden or Collapsed.
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Color x:Key="TabGradientTop">#FFFFFFFF</Color>
<Color x:Key="TabGradientBottom">#FFC0CBE8</Color>
<SolidColorBrush x:Key="TabBarText" Color="#FF353C66"/>
<SolidColorBrush x:Key="TabBarTop" Color="#FFFFFFFF"/>
<SolidColorBrush x:Key="TabBarBottom" Color="#FFC0CBE8"/>
<SolidColorBrush x:Key="TabBarBorder" Color="#FF8E96AC"/>
<SolidColorBrush x:Key="TabMou5eOverColor" Color="#FFFFB700"/>
<Style x:Key="TabStyle" TargetType="{x:Type Button}">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Foreground" Value="{DynamicResource TabBarText}"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Padding" Value="0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<Rectangle x:Name="TabNormal" Fill="Transparent"/>
<Rectangle x:Name="TabMou5eOver" Fill="{StaticResource TabBarBottom}" Height="30" Stroke="{StaticResource TabMou5eOverColor}" StrokeThickness="1" RadiusY="4" RadiusX="4" Visibility="Hidden" />
<Rectangle x:Name="TabMou5eOver2" Fill="{StaticResource TabBarBorder}" Height="1" VerticalAlignment="Bottom" Visibility="Hidden" />
<Rectangle x:Name="TabMou5ePressed" Fill="{StaticResource TabBarTop}" Height="30" Stroke="{StaticResource TabBarBorder}" RadiusY="4" RadiusX="4" Visibility="Hidden" />
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Visibility" TargetName="TabMou5eOver2" Value="Visible"/>
<Setter Property="Visibility" TargetName="TabMou5eOver" Value="Visible"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Visibility" TargetName="TabMou5ePressed" Value="Visible"/>
</Trigger>
<Trigger Property="IsDefaulted" Value="True">
<Setter Property="Visibility" TargetName="TabMou5ePressed" Value="Visible"/>
</Trigger>
<Trigger Property="IsFocused" Value="True">
<Setter Property="Visibility" TargetName="TabMou5ePressed" Value="Visible"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="InvisiStyle" TargetType="{x:Type Button}">
<Setter Property="Foreground" Value="{DynamicResource TabBarText}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Background="Transparent">
<ContentPresenter/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<!--Gadients-->
<StackPanel Orientation="Vertical">
<Rectangle Height="46">
<Rectangle.Fill>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="{StaticResource TabGradientBottom}" Offset="0.25"/>
<GradientStop Color="{StaticResource TabGradientTop}"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<Rectangle Height="1" Fill="{StaticResource TabBarBorder}"/>
<Rectangle Height="87">
<Rectangle.Fill>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="{StaticResource TabGradientBottom}" Offset="0.45" />
<GradientStop Color="{StaticResource TabGradientTop}"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<Rectangle Height="1" Fill="{StaticResource TabBarBottom}" Opacity="0.5"/>
<Rectangle Height="1" Fill="{StaticResource TabBarBottom}"/>
<Rectangle Height="1" Fill="{StaticResource TabBarBorder}"/>
</StackPanel>
<!--Tabs-->
<Grid Margin="15,22,0,0">
<StackPanel Orientation="Horizontal" VerticalAlignment="Top">
<Button x:Name="cmdTab1" Content="Tab1" Style="{DynamicResource TabStyle}" Width="55" Height="25" Margin="10,0,0,0" IsDefault="True"/>
<Button x:Name="cmdTab2" Content="Tab2" Style="{DynamicResource TabStyle}" Width="55" Height="25" Margin="10,0,0,0" />
<Button x:Name="cmdTab3" Content="Tab3" Style="{DynamicResource TabStyle}" Width="55" Height="25" Margin="10,0,0,0" />
<Button x:Name="cmdTab4" Content="Tab4" Style="{DynamicResource TabStyle}" Width="55" Height="25" Margin="10,0,0,0" />
</StackPanel>
</Grid>
<!--Tool Bars-->
<StackPanel x:Name="Tab1Tools" Orientation="Horizontal" Margin="10,52,0,0" Height="80" VerticalAlignment="Top">
<StackPanel Orientation="Vertical">
<Button x:Name="Tab1Login" Content="Login" Height="18" Margin="5,0,0,0" Style="{DynamicResource InvisiStyle}"/>
<Button x:Name="Tab1Request" Content="Request Password" Height="18" Margin="5,0,0,0" Style="{DynamicResource InvisiStyle}"/>
<Rectangle Fill="{StaticResource TabBarBorder}" Height="1" Margin="5,5,5,0"/>
<Rectangle Fill="{StaticResource TabBarTop}" Height="1" Margin="5,0,5,5" Opacity="0.5"/>
<Button x:Name="Tab1Exit" Content="Exit" Height="18" Margin="5,0,0,0" Style="{DynamicResource InvisiStyle}"/>
</StackPanel>
</StackPanel>
<StackPanel x:Name="Tab2Tools" Orientation="Horizontal" Margin="10,52,0,0" Height="80" VerticalAlignment="Top" Visibility="Hidden" />
<StackPanel x:Name="Tab3Tools" Orientation="Horizontal" Margin="10,52,0,0" Height="80" VerticalAlignment="Top" Visibility="Hidden" />
<StackPanel x:Name="Tab4Tools" Orientation="Horizontal" Margin="10,52,0,0" Height="80" VerticalAlignment="Top" Visibility="Hidden" />
<!--Data Grids-->
<DataGrid x:Name="dgTab2" ItemsSource="{Binding}" Margin="0,137,0,0" AutoGenerateColumns="True" Visibility="Collapsed" />
<DataGrid x:Name="dgTab3" ItemsSource="{Binding}" Margin="0,137,0,0" AutoGenerateColumns="True" Visibility="Collapsed" />
<DataGrid x:Name="dgTab4" ItemsSource="{Binding}" Margin="0,137,0,0" AutoGenerateColumns="True" Visibility="Collapsed" />
</Grid>
</Window>
You can bind the button click using Command property in your viewmodel and define a method using delegates. In your method you can set the visibility of datagrids as per the requirement.
Regarding your first query, I simply copied your code on my local and tried executing it, I do not see any tabs becoming invisible while the other one is selected.

WPF glowing button

I have a WPF application. Where I have a toggle button that has a glass effect. When the button is checked the button fills blue which in the code below has been commented out. I have created a rectangle with a glow effect and would like this to be activated when the IsChecked = true. At the moment it doesn't appear to be doing anything.
<Style x:Key="ToggleButtonTemplate" TargetType="ToggleButton">
<Setter Property="Cursor" Value="Hand"/>
<Setter Property="FontSize" Value="12" />
<Setter Property="Foreground" Value="White" />
<Setter Property="Margin" Value="5,5,5,5"/>
<Setter Property="IsChecked" Value="{Binding SecurityList[0].Run}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Border x:Name="ButtonBorder"
CornerRadius="15,15,15,15"
BorderThickness="3,3,3,3"
Background="#AA000000"
BorderBrush="#99FFFFFF"
RenderTransformOrigin="0.5,0.5">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="1.7*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Border Grid.Row="0" Grid.ColumnSpan="2" CornerRadius="23,23,0,0">
<Border.Background>
<LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
<GradientStop Color="#08FFFFFF" Offset="0"/>
<GradientStop Color="#88FFFFFF" Offset="1"/>
</LinearGradientBrush>
</Border.Background>
</Border>
<ContentPresenter x:Name="ButtonContentPresenter"
VerticalAlignment="Center"
Grid.RowSpan="2"
HorizontalAlignment="Center"/>
<Rectangle x:Name="recGlow" HorizontalAlignment="Left" Stroke="Black"
VerticalAlignment="Top" Opacity="0">
<Rectangle.Fill>
<RadialGradientBrush>
<RadialGradientBrush.RelativeTransform>
<TransformGroup>
<ScaleTransform CenterY="0.5" CenterX="0.5" ScaleY="1.248" ScaleX="1.276"/>
<SkewTransform CenterY="0.5" CenterX="0.5"/>
<RotateTransform CenterY="0.5" CenterX="0.5"/>
<TranslateTransform Y="0.317" X="-0.007"/>
</TransformGroup>
</RadialGradientBrush.RelativeTransform>
<GradientStop Color="#FF088CE8" Offset="0"/>
<GradientStop Offset="1"/>
</RadialGradientBrush>
</Rectangle.Fill>
</Rectangle>
<TextBlock Grid.Row="0" Grid.ColumnSpan="2" Text="GBP / USD" HorizontalAlignment="Center" VerticalAlignment="Center" FontWeight="SemiBold"/>
<Image Grid.Row="1" Grid.Column="0" Source="/Resources/GBP.ico"/>
<Image Grid.Row="1" Grid.Column="1" Source="/Resources/USD.ico"/>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<!--<Setter TargetName="ButtonBorder" Property="Background" Value="Blue"/>-->
<Setter TargetName="recGlow" Property="Opacity" Value="0.8"/>
<Setter Property="RenderTransform" TargetName="ButtonBorder">
<Setter.Value>
<TransformGroup>
<ScaleTransform ScaleX="1.1" ScaleY="1.1"/>
</TransformGroup>
</Setter.Value>
</Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Rectangle is too small to be observed.
Set HorizontalAlignment and VerticalAlignment to Stretch and set Grid.ColumnSpan to 2 on rectangle -
<Rectangle x:Name="recGlow" HorizontalAlignment="Stretch" Stroke="Black"
VerticalAlignment="Stretch" Opacity="0" Grid.ColumnSpan="2">

WPF Infinite loop in references found while processing the Template

I am pretty new to WPF and am getting this error after my mouse is over my custom listbox item.
Error: Infinite loop in references found while processing the Template for an element named '' of type 'System.Windows.Controls.TextBox'.
<Window.Resources>
<ControlTemplate x:Key="MouseOverFocusTemplate" >
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="55*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBox Width="290" TextAlignment="Left" VerticalContentAlignment="Center" BorderThickness="0" BorderBrush="Transparent"
Foreground="#FF6FB8FD"
FontSize="24"
TextWrapping="Wrap"
Text="{Binding .}"
Grid.Column="1"
Grid.Row="1"
MinHeight="55"
Cursor="Hand"
IsReadOnly="True"
FontFamily="Arial"
>
<TextBox.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF013B73" Offset="0.501"/>
<GradientStop Color="#FF091F34"/>
<GradientStop Color="#FF014A8F" Offset="0.5"/>
<GradientStop Color="#FF003363" Offset="1"/>
</LinearGradientBrush>
</TextBox.Background>
</TextBox>
</Grid>
</ControlTemplate>
<Style x:Key="MouseOverFocusStyle" TargetType="{x:Type TextBox}">
<Setter Property="Template" Value="{StaticResource MouseOverFocusTemplate}"/>
</Style>
<ControlTemplate x:Key="LostFocusTemplate" >
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="55*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBox Width="290" TextAlignment="Left" VerticalContentAlignment="Center" BorderThickness="0" BorderBrush="Transparent"
Foreground="#FF6FB8FD"
FontSize="24"
TextWrapping="Wrap"
Text="{Binding .}"
Grid.Column="1"
Grid.Row="1"
MinHeight="55"
Cursor="Hand"
IsReadOnly="True"
FontFamily="Arial"
>
<TextBox.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<LinearGradientBrush.RelativeTransform>
<TransformGroup>
<ScaleTransform CenterX="0.5" CenterY="0.5"/>
<SkewTransform CenterX="0.5" CenterY="0.5"/>
<RotateTransform CenterX="0.5" CenterY="0.5"/>
<TranslateTransform/>
</TransformGroup>
</LinearGradientBrush.RelativeTransform>
<GradientStop Color="#FF091F34" Offset="1"/>
<GradientStop Color="#FF002F5C" Offset="0.4"/>
</LinearGradientBrush>
</TextBox.Background>
</TextBox>
</Grid>
</ControlTemplate>
<Style x:Key="LostFocusStyle" TargetType="{x:Type TextBox}">
<Setter Property="Template" Value="{StaticResource LostFocusTemplate}"/>
</Style>
<ControlTemplate x:Key="GotFocusTemplate" >
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="55*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBox Width="290" TextAlignment="Left" VerticalContentAlignment="Center" BorderThickness="0" BorderBrush="Transparent"
Foreground="#FFE38E27"
FontSize="24"
TextWrapping="Wrap"
Text="{Binding .}"
Grid.Column="1"
Grid.Row="1"
MinHeight="55"
Cursor="Hand"
IsReadOnly="True"
FontFamily="Arial"
>
<TextBox.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="0.501"/>
<GradientStop Color="#FF091F34"/>
<GradientStop Color="#FF002F5C" Offset="0.5"/>
</LinearGradientBrush>
</TextBox.Background>
</TextBox>
</Grid>
</ControlTemplate>
<Style x:Key="GotFocusStyle" TargetType="{x:Type TextBox}">
<Setter Property="Template" Value="{StaticResource GotFocusTemplate}"/>
</Style>
<Style TargetType="ListBoxItem">
<EventSetter Event="GotFocus" Handler="ListItem_GotFocus"></EventSetter>
<EventSetter Event="LostFocus" Handler="ListItem_LostFocus"></EventSetter>
<EventSetter Event="Mouse.MouseMove" Handler="ListItem_MouseOver"></EventSetter>
</Style>
<DataTemplate DataType="{x:Type TextBlock}">
</DataTemplate>
<DataTemplate x:Key="CustomListData" DataType="{x:Type ListBoxItem}">
<Border BorderBrush="Black" BorderThickness="1" Margin="-2,0,0,-1">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="55*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleX="1" ScaleY="1"/>
<SkewTransform AngleX="0" AngleY="0"/>
<RotateTransform Angle="0"/>
<TranslateTransform X="0" Y="0"/>
</TransformGroup>
</Grid.RenderTransform>
<!--<ScrollViewer x:Name="PART_ContentHost" />-->
<TextBox Width="290" TextAlignment="Left" VerticalContentAlignment="Center" BorderThickness="0" BorderBrush="Transparent"
Foreground="#FF6FB8FD"
FontSize="24"
FocusVisualStyle="{StaticResource GotFocusStyle}"
TextWrapping="Wrap"
Text="{Binding .}"
Grid.Column="1"
Grid.Row="1"
MinHeight="55"
Cursor="Hand"
IsReadOnly="True"
FontFamily="Arial"
>
<TextBox.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<LinearGradientBrush.RelativeTransform>
<TransformGroup>
<ScaleTransform CenterX="0.5" CenterY="0.5"/>
<SkewTransform CenterX="0.5" CenterY="0.5"/>
<RotateTransform CenterX="0.5" CenterY="0.5"/>
<TranslateTransform/>
</TransformGroup>
</LinearGradientBrush.RelativeTransform>
<GradientStop Color="#FF091F34" Offset="1"/>
<GradientStop Color="#FF002F5C" Offset="0.4"/>
</LinearGradientBrush>
</TextBox.Background>
</TextBox>
</Grid>
</Border>
</DataTemplate>
<Style TargetType="{x:Type ListBox}">
<Setter Property="ItemTemplate" Value="{StaticResource CustomListData }" />
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" />
</Style>
</Window.Resources>
<Window.DataContext>
<ObjectDataProvider
ObjectType="{x:Type local:ImageLoader}"
MethodName="LoadImages"
/>
</Window.DataContext>
<ListBox ItemsSource="{Binding}" Width="320" Background="#FF021422" BorderBrush="#FF1C4B79">
<ListBox.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}">Transparent</SolidColorBrush>
</ListBox.Resources>
</ListBox>
The code behind for the mouse over event is as follows
private void ListItem_MouseOver(object sender, RoutedEventArgs e)
{
e.Handled = true;
FrameworkElement element = e.OriginalSource as FrameworkElement;
if (element != null)
{
while (VisualTreeHelper.GetParent(element) != null)
{
element = VisualTreeHelper.GetParent(element) as FrameworkElement;
TextBox item = element as TextBox;
if (item != null)
{
item.Style = (Style)item.FindResource("MouseOverFocusStyle");
return;
}
}
}
}
What am I missing? Is there an easier way to do this ?
Thanks in advance
Ryan
I think you're trying to do this the hard way, control templates support events (called triggers) see http://msdn.microsoft.com/en-us/library/ms750821%28VS.85%29.aspx
Update: Ok I think I see what your problem is, you're defining a style for type TextBox and then placing a TextBox in the style. You should still be able to combine all your styles into one by using Triggers
Example
<Style x:Key="{x:Type TextBox}" TargetType="{x:Type TextBoxBase}">
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="AllowDrop" Value="true"/>
<Setter Property="Margin" Value="2" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBoxBase}">
<Border
Name="Border"
CornerRadius="2"
Padding="2"
Background="{DynamicResource TextBoxBackBrush}"
BorderBrush="{StaticResource SolidBorderBrush}"
BorderThickness="1" >
<ScrollViewer Margin="0" x:Name="PART_ContentHost"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="Border" Property="Background" Value="{StaticResource PressedBrush}"/>
<Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource DisabledBackgroundBrush}"/>
<Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>

ScrollViewer in a ListBox not working. WPF

I have the following defined in my control:
<Style TargetType="primitives:CalendarDayButton" x:Key="EventCalendarDayButton">
<Setter Property="Background" Value="#FFBADDE9"/>
<Setter Property="MinWidth" Value="5"/>
<Setter Property="MinHeight" Value="5"/>
<Setter Property="FontSize">
<Setter.Value>
<Binding Path="DayFontSize">
<Binding.RelativeSource>
<RelativeSource Mode="FindAncestor" AncestorType="{x:Type toolkit:Calendar}" />
</Binding.RelativeSource>
</Binding>
</Setter.Value>
</Setter>
<Setter Property="HorizontalContentAlignment" Value="Right"/>
<Setter Property="VerticalContentAlignment" Value="Top"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="primitives:CalendarDayButton">
<Grid MouseDown="Grid_MouseDown">
<Grid.RowDefinitions>
<RowDefinition Height="18" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Rectangle x:Name="SelectedBackground" Grid.Row="1" RadiusX="1" RadiusY="1" Opacity="0" Fill="{TemplateBinding Background}"/>
<Rectangle x:Name="Background" Grid.Row="1" RadiusX="1" RadiusY="1" Opacity="0" Fill="{TemplateBinding Background}" />
<Rectangle x:Name="InactiveBackground" Grid.Row="1" RadiusX="1" RadiusY="1" Opacity="0" Fill="#FFA5BFE1"/>
<Border>
<Border.Background>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop x:Name="StartGradient" Color="#FFD5E2F2" Offset="0"/>
<GradientStop x:Name="EndGradient" Color="#FFB9C9DD" Offset="1"/>
</LinearGradientBrush>
</Border.Background>
<ContentPresenter x:Name="NormalText" Margin="5,1,5,1" VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
<TextElement.Foreground>
<SolidColorBrush x:Name="selectedText" Color="#FF333333" />
</TextElement.Foreground>
</ContentPresenter>
</Border>
<Rectangle x:Name="Border" StrokeThickness="0.5" Grid.RowSpan="2" SnapsToDevicePixels="True">
<Rectangle.Stroke>
<SolidColorBrush x:Name="BorderBrush" Color="#FF5D8CC9"/>
</Rectangle.Stroke>
</Rectangle>
<Path x:Name="Blackout" Grid.Row="1" Opacity="0" Margin="3" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" RenderTransformOrigin="0.5,0.5" Fill="#FF000000" Stretch="Fill" Data="M8.1772461,11.029181 L10.433105,11.029181 L11.700684,12.801641 L12.973633,11.029181 L15.191895,11.029181 L12.844727,13.999395 L15.21875,17.060919 L12.962891,17.060919 L11.673828,15.256231 L10.352539,17.060919 L8.1396484,17.060919 L10.519043,14.042364 z"/>
<Rectangle Width="0" x:Name="DayButtonFocusVisual" Grid.Row="1" Visibility="Collapsed" IsHitTestVisible="false" RadiusX="1" RadiusY="1" Stroke="#FF45D6FA"/>
<Button x:Name="ActivateDayViewOnDay" Grid.Row="0" Opacity="0.3" Height="15" Margin="1,1,1,1" PreviewMouseLeftButtonDown="DayView_Click" />
<ScrollViewer Grid.Row="1" >
<ScrollViewer.Content>
<local:TestListBox
x:Name="eventsLbx"
Background="Transparent"
BorderBrush="Transparent"
>
<local:TestListBox.ItemsSource>
<MultiBinding Converter="{StaticResource calendarEventsConverter}">
<Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type local:EventCalendar}}" Path="CalendarEvents"/>
<Binding RelativeSource="{RelativeSource Mode=Self}" Path="DataContext"/>
</MultiBinding>
</local:TestListBox.ItemsSource>
<local:TestListBox.ItemTemplate>
<DataTemplate>
<TextBlock TextTrimming="CharacterEllipsis" HorizontalAlignment="Center" FontSize="12" Text="{Binding Text}" />
</DataTemplate>
</local:TestListBox.ItemTemplate>
</local:TestListBox>
</ScrollViewer.Content>
</ScrollViewer>
</Grid>
<ControlTemplate.Triggers>
<Trigger SourceName="eventsLbx" Property="HasItems" Value="False">
<Setter TargetName="eventsLbx" Property="Visibility" Value="Hidden"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Now if there are more items than are visible, then the scrollviewer appears properly but the user CANNOT drag the scrollviewer middle button for scrolling.
The user can click on the arrows at the end of the scrollviewer to scroll but he cannot click the bar that appears on the scrollbar and drag it to actually scroll the contents.
I cannot figure out why this is happening...
If you want to scroll the items in your ListBox, you do not need to wrap it in a ScrollViewer. The ListBox natively supports scrolling. That means, if your ListBox is too small to display all its items, it automatically adds ScrollBars to the side.
Your ScrollViewer and ListBox are inside a Grid, and you have some code behind the MouseDown event of that Grid.
<Grid MouseDown="Grid_MouseDown">
Something you're doing in that method may be getting in the way of the mouse mechanisms of the ScrollViewer, disrupting event bubbling or something like that.
Check your code-behind, or post it here so we can help you with it :)

Resources