0

(For the back story about this question please see Question Here) To continue learning about WPF I am trying to Extend an TextBox Control and add a Dependency Property which can be used in a Style to put a border around my extended TextBox Control. To help me achieve this I am using this article basics-of-extending-a-wpf-control.

I have encountered two problems:
1. When I put the style into a Resource Dictionary and try to merge the Resource Dictionary with a file called "generic.xaml" (also tried "Generic.xaml") the style does not take, upon reading this question here trouble-referencing-a-resource-dictionary-that-contains-a-merged-dictionary I tried the solution of adding a Style referencing any control but it did not work (all code at the bottom of the question}. Can anyone explain why please?
2. To overcome the problem above I moved my Style into a Window Resource, but I encounted a different problem. I purposely left out the following code from the worked example in my Constructor of my Extended Control

static ExTextBox()
{
    DefaultStyleKeyProperty.OverrideMetadata(typeof(ExTextBox),
                  new FrameworkPropertyMetadata(typeof(ExTextBox)));
}

as I was expecting my control to inherit the default style of the Standard TextBox, which includes the Properties BorderThickness and BorderBrush. However, when I try and Set these Properties with the Setter statement vs2010 says it can not find the Properties. Can someone explain why and if these Properties are not inherited do I have to use a Control Template in my Style to set up a Border around my Control?

My code from problem 1 MainWindow.xaml

<Window x:Class="Controls.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="StyleTriggersSample" Height="100" Width="300" xmlns:my="clr-namespace:Controls">

    <Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="138*" />
        <ColumnDefinition Width="140*" />
    </Grid.ColumnDefinitions>
    <my:ExTextBox  Height="23" HorizontalAlignment="Left" Margin="12,28,0,0" x:Name="exTextBox1" Text="ExTextBlock" VerticalAlignment="Top" Width="116" />
    <TextBox Grid.Column="1" Height="23" HorizontalAlignment="Left" Margin="14,30,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" Text="textBox1" />
    <Button Content="Test" Grid.Column="1" Height="23" HorizontalAlignment="Left" Margin="14,0,0,0" Name="button1" VerticalAlignment="Top" Width="114" Click="button1_Click" />
    </Grid>

My Extended Control

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;


namespace Controls
{
public class ExTextBox : TextBlock
{
    public static  DependencyProperty ErrorFlagProperty =
      DependencyProperty.Register("ErrorFlag", typeof(bool),
                   typeof(ExTextBox),
                    new FrameworkPropertyMetadata((bool)false) );

    public bool ErrorFlag
    {
        get { return (bool)GetValue(ErrorFlagProperty); }
        set { SetValue(ErrorFlagProperty, value); }
    }

    static ExTextBox()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(ExTextBox),
              new FrameworkPropertyMetadata(typeof(ExTextBox)));
     }

   }

 }

Controls.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="Controls:ExTextBox" BasedOn="{StaticResource {x:Type TextBox}}">
    <Setter Property="FontSize" Value="28" />
    <Setter Property="HorizontalAlignment" Value="Center" />
    <Setter Property="VerticalAlignment" Value="Center" />
    <Setter Property="BorderBrush" Value="Silver" />
    <Setter Property="Height" Value="50" />
    <Setter Property="Width" Value="120" />
    <Style.Triggers>
        <Trigger Property="IsMouseOver"
                 Value="True">
            <Setter Property="BorderThickness"
                    Value="5" />
        </Trigger>
    </Style.Triggers>
</Style>
</ResourceDictionary>

generic.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="/TestContentStyle;component/Controls.xaml" />
</ResourceDictionary.MergedDictionaries>-->
<!-- Dummy Style, anything you won't use goes -->
<Style TargetType="{x:Type Rectangle}" />

Community
  • 1
  • 1
Ian W
  • 385
  • 2
  • 10
  • 30

0 Answers0