How to change shape of thumb to ellips in wpf

  • Thread starter Thread starter moondaddy
  • Start date Start date
M

moondaddy

I'm writing a windows app in WPF and want to change the shape of a thumb to
an ellipse. Is this possible? also, the edges of the thumb are beveled.
Is it possible to change this to a flat look and if so, how?

Here's how I'm creating my thumb so far.

cornerThumb = new Thumb();
// Set some arbitrary visual characteristics.
cornerThumb.Cursor = customizedCursor;
cornerThumb.Height = cornerThumb.Width = 10;
cornerThumb.Opacity = 0.40;
cornerThumb.Background = new SolidColorBrush(Colors.MediumBlue);

Thanks for any input.
 
You'll probably get better help in the Avalon newsgroup (or the MSDN
Forums), but yes, it's possible. It's easier to represent in XAML, but what
you want to do is override the visual template with your new visual:

<Thumb Height="10" Width="10" Opacity=".4" Background="MediumBlue">
<Thumb.Style>
<Style TargetType={x:Type Thumb}>
<Style.Setters>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Thumb}">
<Ellipse.../>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style.Setters>
</Style>
</Thumb.Style>
<Thumb.Cursor>
</Thumb.Cursor>
</Thumb>
 
Thanks Keith. I hope you don't mind that I revised your code a little as
follows:

<Window x:Class="WindowsApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WindowsApplication1" Height="300" Width="300"
<Thumb Height="10" Width="10" Opacity=".4" Background="MediumBlue">
<Thumb.Style>
<Style TargetType="{x:Type Thumb}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Thumb}">
<Ellipse Width="10" Height="5" Stroke="Blue"
StrokeThickness="2"></Ellipse>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Thumb.Style>
</Thumb>
</Window>

#Styling and Templating
http://msdn2.microsoft.com/en-us/library/ms745683.aspx


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
As you may already have known, you could still define the style (template)
in XAML. Then you could use FindResource (method of FrameworkElement) to
load the resource and apply it to the Thumb object that you've created
using code:

<Grid Name="grid1">
<Grid.Resources>
<Style x:Key="ThumbStyle" TargetType="{x:Type Thumb}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Thumb}">
<Ellipse Width="10" Height="5" Stroke="Blue" Fill="Blue"
StrokeThickness="2"></Ellipse>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
<Thumb Name="thumb1" Height="10" Width="10" Opacity=".4"
Background="MediumBlue">
</Thumb>
</Grid>


Code:

thumb1.Style = (Style) grid1.FindResource("ThumbStyle");

Hope this helps.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Thanks Walter that did it. In my case I added the style to a resource file.
In order to refrence the style from c#, I had to add a reference to the file
in the window's xaml like this:

<Window.Resources>
<ResourceDictionary Source="Dictionary1.xaml" />
</Window.Resources>

Here's the style in Dictionary1.xaml:

<Style x:Key="ThumbStyle" TargetType="{x:Type Thumb}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Thumb}">
<Ellipse Width="15" Height="15" Stroke="DarkSlateGray"
StrokeThickness="0.5">
<Ellipse.Fill >
<SolidColorBrush Color="DarkBlue" Opacity="0.2"></SolidColorBrush>
</Ellipse.Fill>
<Ellipse.BitmapEffect>
<DropShadowBitmapEffect Color="Black" Direction="320"
ShadowDepth="5" Softness="0.5" Opacity="0.2" />
</Ellipse.BitmapEffect>
</Ellipse>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

and here's the c# that used it:

Thumb tb = new Thumb();
Canvas.SetLeft(tb, 100);
Canvas.SetTop(tb, 100);
myCanvas.Children.Add(tb);
tb.Style = (Style)this.FindResource("ThumbStyle");
 
Hi moondaddy,

You probably have seen my reply in your another post regarding this
FindResource question. Anyway, I'm including the reply here again for your
reference:

-----------

When finding a resource, it first checks the current element's Resources
collection (its resource dictionary). If the item is not found, it checks
the parent element, its parent, and so on until it reaches the root
element. At that point, it checks the Resources collection on the
Application object. If it is not found there, it finally checks a system
collection (which contains system-defined fonts, colors, and other
settings). If the item is in none of these collections, it throws an
InvalidOperationException.

Therefore you could simply put resources in App.xaml within
<Application.Resources> and those resources will be shared by all your code
in the same assembly.

For ResourceDictionary defined in separate .xaml files, you could use
following code to load it:

ResourceDictionary rd = new ResourceDictionary();
rd.Source = new Uri(@"Dictionary1.xaml", UriKind.Relative);

Then you can use rd["resource_key"] to reference each resource.


-------------

Hope this helps.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top