Customize the Style of RichTextBox itself [duplicate] - wpf

I'm trying to create a new custom control, which inherits from RichTextBox. The reason for this is to add custom buffering to the control (e.g. only append text every x milliseconds and/or buffer.Length > x).
I've managed to create the control and add it to the xaml Window, however it doesn't seem to actually operate as a RichTextBox properly - text doesn't display after being appended, and the cursor doesn't change icon when hovered over the control.
It seems to be fairly simple code, so I'm not sure where I'm going wrong.
CBufferedTextBox.cs:
public class CBufferedTextBox : RichTextBox
{
const int MAX_LENGTH = 2048;
const int TIMER_LENGTH = 1000;
DispatcherTimer m_timer = new DispatcherTimer();
DispatcherTimer Timer
{
get { return m_timer; }
}
StringBuilder m_currentText = new StringBuilder();
StringBuilder CurrentText
{
get { return m_currentText; }
}
static CBufferedTextBox()
{
DefaultStyleKeyProperty.OverrideMetadata( typeof( CBufferedTextBox ), new FrameworkPropertyMetadata( typeof( CBufferedTextBox ) ) );
}
public CBufferedTextBox()
{
Loaded += CBufferedTextBox_Loaded;
}
public CBufferedTextBox( FlowDocument document )
: base( document )
{
}
public new void AppendText( string strText )
{
CurrentText.Append( strText );
if( !strText.EndsWith( Environment.NewLine ) )
{
CurrentText.AppendLine();
}
if( CurrentText.Length > MAX_LENGTH )
{
Flush();
}
}
void CBufferedTextBox_Loaded( object sender, RoutedEventArgs e )
{
Timer.Interval = new TimeSpan( TIMER_LENGTH );
Timer.Tick += new EventHandler( Timer_Tick );
Timer.Start();
}
void Timer_Tick( object sender, EventArgs e )
{
Flush();
}
void Flush()
{
Timer.Stop();
this.BeginInvokeIfRequired( o =>
{
if( CurrentText.Length > 0 )
{
base.AppendText( CurrentText.ToString() );
// Clear
CurrentText.Remove( 0, CurrentText.Length );
ScrollToEnd();
}
Timer.Start();
} );
}
}
Generic.xaml:
<Style TargetType="{x:Type local:CBufferedTextBox}" BasedOn="{StaticResource {x:Type RichTextBox}}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CBufferedTextBox}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Thanks,
Rich

Well of course not, you completely have overriden the Style and especially the ControlTemplate. Your Control just consists of a Border, thats all. No text input, no text display no nothing. You need to implement at least the bare basics in your template, to let your CBufferedTextBox behave like you expect.
I also want to point out, that your new void AppendText is pretty dangerous and might not do what you expect. In your Flush method you call the AppendText of the RichtText box not yours. Also new is not the same as override. The RichTextBox will never call your method internally, even if it is of your new type CBufferedTextBox.

Related

How to set DP value when the custom control use in the xaml? (when declaring)

My English skill is poor because I'm not a native English speaker.
I hope you to understand.
I created a custom window that overrides the title bar shape.
The part of the xaml code is as shown below.
<Style x:Key="MainWindow" TargetType="{x:Type Window}">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}" />
<Setter Property="WindowStyle" Value="None"/>
<Setter Property="AllowsTransparency" Value="True"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Window}">
<Grid>
<Border x:Name="PART_TITLEBAR"
Margin="2,0,2,2"
Height="30"
DockPanel.Dock="Top"
CornerRadius="2"
Background="Transparent">
This control works well except for one problem.
The problem is that can't set the value of the DP.
The part of the cs code of the control is as shown below.
[TemplatePart(Name = "PART_TITLEBAR", Type = typeof(UIElement))]
public partial class CustomWindow : Window
{
private UIElement TitleBar { get; set; }
#region Dependency Properties for appearance.
public int TitleBarHeight
{
get { return (int)GetValue(TitleBarHeightProperty); }
set { SetValue(TitleBarHeightProperty, value); }
}
// Using a DependencyProperty as the backing store for TitleBarHeight. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TitleBarHeightProperty =
DependencyProperty.Register("TitleBarHeight", typeof(int), typeof(CustomWindow), new PropertyMetadata(TitleBarHeightChanged));
public static void TitleBarHeightChanged(DependencyObject dp, DependencyPropertyChangedEventArgs args)
{
CustomWindow window = dp as CustomWindow;
Border titleBar = window.TitleBar as Border;
if (titleBar == null) return;
titleBar.Height = (int)args.NewValue;
}
public SolidColorBrush TitleTextBrush
{
get { return (SolidColorBrush)GetValue(TitleTextBrushProperty); }
set { SetValue(TitleTextBrushProperty, value); }
}
// Using a DependencyProperty as the backing store for TitleTextBrush. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TitleTextBrushProperty =
DependencyProperty.Register("TitleTextBrush", typeof(SolidColorBrush), typeof(CustomWindow), new PropertyMetadata(TitleTextBrushChanged));
public static void TitleTextBrushChanged(DependencyObject dp, DependencyPropertyChangedEventArgs args)
{
CustomWindow window = dp as CustomWindow;
Border titleBar = window.TitleBar as Border;
if (titleBar == null) return;
// find the textblock control of the children of the titlebar and change the value of the foreground of the control.
}
#endregion
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
AttachToVisualTree();
}
private void AttachToVisualTree()
{
AttachCloseButton();
AttachMinimizeButton();
AttachMaximizeRestoreButton();
AttachTitleBar();
AttachBorders();
}
private void AttachTitleBar()
{
if (TitleBar != null)
{
TitleBar.RemoveHandler(UIElement.MouseLeftButtonDownEvent, new MouseButtonEventHandler(OnTitlebarClick));
}
UIElement titleBar = GetChildControl<UIElement>("PART_TITLEBAR");
if (titleBar != null)
{
TitleBar = titleBar;
titleBar.AddHandler(UIElement.MouseLeftButtonDownEvent, new MouseButtonEventHandler(OnTitlebarClick));
}
}
I tried to track the problem and I have found the cause.
First, I loaded the custom control in the main project and set the value of the DP on the custom control as below.
<custom:CustomWindow TitleBarHeight="20">
<.../>
</custom:CustomWindow>
And then later, I executed the project and the sequence processed as below.
The CustomWindow is created. (constructor called)
The TitleBarHeight value of the CustomWindow is set
OnApplyTemplate() of the CustomWindow is called.
According to my confirmation, sequence 2 is the starting point of the problem.
In sequence 2, WPF trying to set the TitleBarHeight value of the CustomWindow. therefore the below code is called.
public static void TitleBarHeightChanged(DependencyObject dp, DependencyPropertyChangedEventArgs args)
{
CustomWindow window = dp as CustomWindow;
Border titleBar = window.TitleBar as Border;
if (titleBar == null) return;
titleBar.Height = (int)args.NewValue;
}
But at this point, the TitleBar has not be instantiated so TitleBarHeight value is not set.
As a result, it would be moved to the routine of the below.
if (titleBar == null) return;
After then later, OnApplyTemplate() is called and TitleBar is instantiated.
Summary :
when execute < custom:CustomWindow TitleBarHeight="20"> logic, at this point the TitleBar of the CustomWindow is not instantiated so TitleBarHeight value is not set.
What I should do to solve this problem?
I hope to get your help.
Thank you for reading.
Thanks for the advice, I solved this problem.
I modified xaml code as below.
<Border x:Name="PART_TITLEBAR"
Margin="2,0,2,2"
Height="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:CustomWindow}, Path=TitleBarHeight}"
DockPanel.Dock="Top"
CornerRadius="2"
Background="Transparent">
If you have a better way of doing this, please let me know.
Thank you for advice.

Workaround for display bug of TimeSpanUpDown when changing days

The TimeSpanUpDown (Extended WPF Toolkit) seems to have a display bug when the number of days changes from 0 to >0.
Here is a simple way to reproduce it:
<Window x:Class="TimeSpanBug.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
Height="100" Width="200">
<StackPanel>
<!-- Bind both to the same TimeSpan property in the ViewModel -->
<xctk:TimeSpanUpDown Value="{Binding TimeSpan}"/>
<xctk:TimeSpanUpDown Value="{Binding TimeSpan}"/>
</StackPanel>
</Window>
Enter a time span close to but below 24h. The number of days is automatically hidden.
Then press the up-arrow on the first control to increase the time span to >24h. The control now updates its display to include the number of days. The second control receives the property changed notification and also tries to update, but ends up in a weird state:
Obviously, this is a bug and should be fixed by Xceed, but does anyone know a quick and easy fix or workaround?
Why not roll your own? Welcome to custom control authoring! A simple starting point without the above bug, which you can customize to your heart's content:
[TemplatePart(Name = "UP", Type = typeof(ButtonBase))]
[TemplatePart(Name = "DOWN", Type = typeof(ButtonBase))]
public class TimeSpinner : Control
{
static TimeSpinner()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(TimeSpinner), new FrameworkPropertyMetadata(typeof(TimeSpinner)));
}
public TimeSpan Time
{
get { return (TimeSpan)GetValue(TimeProperty); }
set { SetValue(TimeProperty, value); }
}
// Using a DependencyProperty as the backing store for Time. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TimeProperty =
DependencyProperty.Register("Time", typeof(TimeSpan), typeof(TimeSpinner), new PropertyMetadata(TimeSpan.Zero));
public TimeSpan Interval
{
get { return (TimeSpan)GetValue(IntervalProperty); }
set { SetValue(IntervalProperty, value); }
}
// Using a DependencyProperty as the backing store for Interval. This enables animation, styling, binding, etc...
public static readonly DependencyProperty IntervalProperty =
DependencyProperty.Register("Interval", typeof(TimeSpan), typeof(TimeSpinner), new PropertyMetadata(TimeSpan.FromTicks(TimeSpan.TicksPerHour)));
private static readonly TimeSpan OneDay = TimeSpan.FromTicks(TimeSpan.TicksPerDay);
void UpTime()
{
var newTime = Time + Interval;
if (newTime >= OneDay)
Time = newTime - OneDay;
else
Time = newTime;
}
void DownTime()
{
var newTime = Time - Interval;
if (newTime < TimeSpan.Zero)
Time = newTime + OneDay;
else
Time = newTime;
}
public override void OnApplyTemplate()
{
var upButton = GetUIPart<ButtonBase>("UP");
if(upButton != null)
upButton.Click += upButton_Click;
if (_upButton != null)
_upButton.Click -= upButton_Click;
_upButton = upButton;
var downButton = GetUIPart<ButtonBase>("DOWN");
if (downButton != null)
downButton.Click += downButton_Click;
if (_downButton != null)
_downButton.Click -= downButton_Click;
_downButton = downButton;
}
void downButton_Click(object sender, RoutedEventArgs e)
{
DownTime();
}
void upButton_Click(object sender, RoutedEventArgs e)
{
UpTime();
}
private ButtonBase _upButton;
private ButtonBase _downButton;
T GetUIPart<T>(string name) where T : DependencyObject
{
return (T) GetTemplateChild(name);
}
}
and a sample template (which should put in a ResourceDictionary called Generic.xaml in a folder called Themes at the root of your project):
<Style TargetType="{x:Type local:TimeSpinner}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:TimeSpinner}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<DockPanel>
<UniformGrid Columns="1" DockPanel.Dock="Right">
<Button x:Name="UP" Content="+"/>
<Button x:Name="DOWN" Content="-"/>
</UniformGrid>
<TextBox Text="{Binding Time, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"/>
</DockPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

WPF custom textbox takes two tabs to get to text?

I have a custom textbox control that displays its name when there is no text inside of it, but for some curious reason I have to hit tab twice to get from the previous element to get into the control's text field. On the first tab it highlight's the TextBox's border. I went through all of the levels of the Generic.xaml file with the properties window open and searching for 'tab' but the only one that I could find was on the TextBox itself which is properly tabstopping. How do I make my control only take one tab to get into the
Generic.xaml:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SuperTB">
<Style TargetType="{x:Type local:SuperTextB}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:SuperTextB}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<TextBox Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Text, Mode=TwoWay, UpdateSourceTrigger=LostFocus }" x:Name="PART_input">
</TextBox>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
CS:
[TemplatePart(Name="PART_input")]
public class SuperTextB : Control
{
private TextBox PART_input;
static SuperTextB()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(SuperTextB), new FrameworkPropertyMetadata(typeof(SuperTextB)));
}
public SuperTextB()
{
Loaded += SuperTextBLoaded;
}
void SuperTextBLoaded(object sender, RoutedEventArgs e)
{
if (PART_input.Text == string.Empty)
{
PART_input.Background = convertName();
}
}
public override void OnApplyTemplate()
{
PART_input = GetTemplateChild("PART_input") as TextBox;
if (PART_input != null)
{
PART_input.GotFocus += PartInputGotFocus;
PART_input.LostFocus += PartInputLostFocus;
}
}
void PartInputLostFocus(object sender, RoutedEventArgs e)
{
if (PART_input.Text == string.Empty)
{
PART_input.Background = convertName();
}
}
private VisualBrush convertName()
{
char[] pieces = Name.ToCharArray();
for (int x = 0; x < pieces.Length; x++)
{
if (pieces[x].Equals('_'))
pieces[x] = ' ';
}
String toReturn = "";
foreach (char c in pieces)
toReturn += c.ToString();
VisualBrush myVis = new VisualBrush();
myVis.Stretch = Stretch.None;
TextBlock myText = new TextBlock();
myText.Text = toReturn;
myText.Foreground=Brushes.Gray;
myVis.Visual=myText;
return myVis;
}
void PartInputGotFocus(object sender, RoutedEventArgs e)
{
PART_input.Background = Brushes.White;
}
public static DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(String), typeof(SuperTextB));
public string Text
{
get { return (String)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
}
}
It's because you have a TextBox within the template for your TextBox. That TextBox is focusable, as is your outer TextBox. Set IsFocusable to False or alter your template such that it doesn't include a TextBox within it.
simply put <Setter Property="Focusable" Value="False"/>
in between <Style TargetType="{x:Type local:SuperTextB}">
<Setter Property="Template"> will fix it.

How to add events to templated control in silverlight?

Bounty Rewarded for any solid tutorial/learning resources regarding wiring up events with templated controls.
I Have a control template like this:
<Style TargetType="local:DatePicker">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:DatePicker">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}" x:Name="myDatePickerContentArea">
<StackPanel Orientation="Vertical">
<Button x:Name="myTestButton" Content="Test button" />
<telerik:RadDatePicker Style="{StaticResource VisitsReportTextBoxStyle}" Foreground="#FFFFFF" x:Name="startDate" DateTimeWatermarkContent="Start Date"/>
<telerik:RadDatePicker Style="{StaticResource VisitsReportTextBoxStyle}" x:Name="endDate" DateTimeWatermarkContent="End Date"/>
</StackPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
The C# for this template is:
public class DatePicker : Control
{
public static readonly DependencyProperty StartDateSelectedDateProperty = DependencyProperty.Register("StartDateSelectedDateProperty", typeof(DateTime), typeof(DatePicker), null);
public DateTime? StartDateSelectedDate { get; set; }
public DatePicker()
{
this.DefaultStyleKey = typeof(DatePicker);
}
public override void OnApplyTemplate()
{
RadDatePicker StartDate = this.GetTemplateChild("startDate") as RadDatePicker;
StartDate.SelectionChanged += new Telerik.Windows.Controls.SelectionChangedEventHandler(StartDate_SelectionChanged);
StartDate.SelectedDate = new DateTime(2010, 01, 01);
base.OnApplyTemplate();
}
void StartDate_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangedEventArgs e)
{
RadDatePicker temp = (RadDatePicker)sender;
StartDateSelectedDate = temp.SelectedDate;
}
}
My selectionChanged Event Doesn't Fire and I'm not sure why.
Any Ideas ?
Here is an example of using best practice with the sort of control I think you are attempting to build (see notes at end for some explanations):-
[TemplatePart(Name = DatePicker.ElementStartDate, Type = typeof(RadDatePicker))]
[TemplatePart(Name = DatePicker.ElementEndDate, Type = typeof(RadDatePicker))]
public class DatePicker : Control
{
public DatePicker()
{
this.DefaultStyleKey = typeof(DatePicker);
}
#region Template Part Names
private const string ElementStartDate = "startDate";
private const string ElementEndDate = "endDate";
#endregion
#region Template Parts
private RadDatePicker _StartDate;
internal RadDatePicker StartDate
{
get { return _StartDate; }
private set
{
if (_StartDate != null)
{
_StartDate.SelectionChanged -= StartDate_SelectionChanged;
}
_StartDate = value;
if (_StartDate != null)
{
_StartDate.SelectionChanged += StartDate_SelectionChanged;
}
}
}
private RadDatePicker _EndDate;
internal RadDatePicker EndDate
{
get { return _EndDate; }
private set
{
if (_EndDate!= null)
{
_EndDate.SelectionChanged -= EndDate_SelectionChanged;
}
_EndDate= value;
if (_EndDate!= null)
{
_EndDate.SelectionChanged += EndDate_SelectionChanged;
}
}
}
#endregion
public static readonly DependencyProperty StartDateSelectedDateProperty =
DependencyProperty.Register(
"StartDateSelectedDateProperty",
typeof(DateTime?),
typeof(DatePicker),
new PropertyMetaData(new DateTime(2010, 01, 01)));
public DateTime? StartDateSelectedDate
{
get { return (DateTime?)GetValue(StartDateSelectedDateProperty); }
set { SetValue(StartDateSelectedDateProperty)}
}
public static readonly DependencyProperty EndDateSelectedDateProperty =
DependencyProperty.Register(
"EndDateSelectedDateProperty",
typeof(DateTime?),
typeof(DatePicker),
new PropertyMetaData(new DateTime(2010, 01, 01)));
public DateTime? EndDateSelectedDate
{
get { return (DateTime?)GetValue(EndDateSelectedDateProperty); }
set { SetValue(EndDateSelectedDateProperty)}
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
StartDate = GetTemplateChild(ElementStartDate) as RadDatePicker;
EndDate = GetTemplateChild(ElementEndDate) as RadDatePicker;
}
void StartDate_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangedEventArgs e)
{
// Do stuff with StartDate here
}
void EndDate_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangedEventArgs e)
{
// Do stuff with EndDate here
}
}
The template Xaml should look like:-
<Style TargetType="local:DatePicker">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:DatePicker">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}" x:Name="myDatePickerContentArea">
<StackPanel Orientation="Vertical">
<Button x:Name="myTestButton" Content="Test button" />
<telerik:RadDatePicker x:Name="startDate"
Style="{StaticResource VisitsReportTextBoxStyle}"
Foreground="#FFFFFF"
DateTimeWatermarkContent="Start Date"
SelectedDate="{TemplateBinding StartDateSelectedDate}"
/>
<telerik:RadDatePicker x:Name="endDate"
Style="{StaticResource VisitsReportTextBoxStyle}"
DateTimeWatermarkContent="End Date"
SelectedDate="{TemplateBinding EndDateSelectedDate}"
/>
</StackPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Some Explanations
A key problem your original code had was that it hadn't implemented the dependency properties correctly. Note that properties now use GetValue and SetValue, also that the property meta data is used to assign the default rather than attempting to set in in onapplytemplate.
With the properties correctly implemented the template binding should work and in fact we're done as far as getting what appears to be your original intent, hence I've left out any actual code in the event handlers.
Create constants in the code to hold the names of key template parts that you want to interact with, this allows for name changes to be less expensive to make.
Add TemplatePart attributes to the class to indicate the key elements that the code expects to find, what their names should be and what base type they are expected to have. This allows for a designer to re-template an existing control, as long the declared template parts are present somewher the control should function correctly even if its UI is radically altered.
If you need to attach event handlers for some elements create a field to hold a reference to the element and then create a property to wrap round it. The property setter should then detach and attach the event handlers as you see in the code.
Make sure bae.OnApplyTemplate is called in the override of OnApplyTemplate then as you can see its quite straight forward to assign the above created properties.
I don't have the RadDatePicker so I can't test, my only outstanding concern is where the DateTime? is the correct type for the SelectedDate property. Certainly if it is its an improvement over the Microsoft offering which seems to have drop the ball on this typical data entry requirement.
I may only guess that the problem is that for OnApplyTemplate method Implementers should always call the base implementation before their own implementation.
The other thing is that from your code it looks like it's better to use TemplateBinding(Archive)(V4) in the template xaml
<telerik:RadDatePicker SelectedDate={TemplateBinding StartDateSelectedDate}
Style="{StaticResource VisitsReportTextBoxStyle}"
Foreground="#FFFFFF" x:Name="startDate"
DateTimeWatermarkContent="Start Date"/>

Increasing or decreasing control fontsize on runtime according to Slider(WPF component) value

I want to increase or decrease font size of controls such as window, treeView, ribbon menu etc that are contained by main window.
I have a font size slider create method and I want to acces all of Control and TextBlock by using visualtree helper and increase or decrease their font size according to slider value.
Methods are below;
private StackPanel CreateFontSizeSlider()
{
StackPanel fontSizePanel = new StackPanel();
fontSizePanel.Orientation = Orientation.Horizontal;
Slider fontSizeSlider = new Slider();
fontSizeSlider.Minimum = -3;
fontSizeSlider.Maximum = 5;
fontSizeSlider.Value = 0;
fontSizeSlider.Orientation = Orientation.Horizontal;
fontSizeSlider.TickPlacement = System.Windows.Controls.Primitives.TickPlacement.TopLeft;
fontSizeSlider.IsSnapToTickEnabled = true;
fontSizeSlider.ValueChanged += new RoutedPropertyChangedEventHandler<double>(fontSizeSlider_ValueChanged);
fontSizeSlider.Width = 150;
fontSizePanel.Children.Add(fontSizeSlider);
return fontSizePanel;
}
public static void ChangeControlsFontSize(DependencyObject dependencyObject, double value)
{
foreach (DependencyObject childItem in GetChildren(dependencyObject))
{
if (childItem is Control)
{
Control control = childItem as Control;
control.FontSize = control.FontSize + value;
}
else if (childItem is TextBlock)
{
((TextBlock)childItem).FontSize = ((TextBlock)childItem).FontSize + value;
}
ChangeControlsFontSize(childItem, value);
}
}
private static IEnumerable<DependencyObject> GetChildren(DependencyObject reference)
{
int childCount = VisualTreeHelper.GetChildrenCount(reference);
for (int i = 0; i < childCount; i++)
{
yield return VisualTreeHelper.GetChild(reference, i);
}
}
private void fontSizeSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
ChangeControlsFontSize(this, e.NewValue - e.OldValue);
}
There are some problems;
Firstly I can not acces all controls by walking visual tree. For example I cannot acces closed ribbon menu items. So I can not change their fonts.
Secondly some controls contain another controls so I increase or decrease control font size twice.
Is there any solution for these proplems or is there another way to do this ? Could you help me please ?
You are working way too hard. :-D
Create a style like this:
<Style TargetType="ListBox">
<Setter Property="FontFamily" Value="Tahoma"/>
<Setter Property="FontSize">
<Setter.Value>
<Binding Source="{x:Static Application.Current}" Path="fontSize"/>
</Setter.Value>
</Setter>
</Style>
Create a property called fontSize on your application.
Make a slider like this:
<Slider Name="fontSize" Minimum="10" Maximum="22" IsSnapToTickEnabled="True" TickPlacement="TopLeft"
Value="{Binding Source={x:Static Application.Current}, Path=fontSize, Mode=TwoWay}"/>
Now, any control with that style will nicely resize - and no code is needed!

Resources