I am new to this forum. I have a custom user control defined using c# and xaml. When i dag and drop this control to WPF window it works. Even i can edit xaml code tags and insert my control. But when i use my control in c# code, it dont work.
here is my xaml control definition
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:local="clr-namespace:UserControl"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<!-- Resource dictionary entries should be defined here. -->
<Style TargetType="{x:Type local:WellImage}">
<Setter Property="Focusable" Value="false" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:WellImage}">
<Grid Width="Auto" Height="Auto">
<Ellipse Stroke="{Binding Path=WellBorder, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}"
StrokeThickness="{Binding Path=WellBorderThickness, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}"
x:Name="Border" Width="Auto" Height="Auto"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
Fill="{Binding Path=OuterBackGround, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" />
<Ellipse StrokeThickness="0" Margin="25,37,25,18" RenderTransformOrigin="0.5,0.5"
Fill="{Binding Path=InnerBackGround, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
and here is my c# control definition
using System;
using System.Collections.Generic;
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.Shapes;
namespace UserControl
{
public class WellImage : System.Windows.Controls.Button
{
public static readonly DependencyProperty InnerBackGroundProperty = DependencyProperty.Register("InnerBackGround", typeof(RadialGradientBrush), typeof(WellImage));
public static readonly DependencyProperty OuterBackGroundProperty = DependencyProperty.Register("OuterBackGround", typeof(RadialGradientBrush), typeof(WellImage));
public static readonly DependencyProperty WellBorderProperty = DependencyProperty.Register("WellBorder", typeof(SolidColorBrush), typeof(WellImage));
public static readonly DependencyProperty WellBorderThicknessProperty = DependencyProperty.Register("WellBorderThickness", typeof(double), typeof(WellImage));
public WellImage()
{
// Insert code required on object creation below this point.
InnerBackGround = (RadialGradientBrush)this.Resources["WellSelectedInnerCircleBrush"];
OuterBackGround = (RadialGradientBrush)this.Resources["WellSelectedOuterCircleBrush"];
WellBorder = (SolidColorBrush)this.Resources["NormalBackgroundBrush"];
WellBorderThickness =2;
}
static WellImage()
{
//This OverrideMetadata call tells the system that this element wants to provide a style that is different than its base class.
//This style is defined in themes\generic.xaml
DefaultStyleKeyProperty.OverrideMetadata(typeof(WellImage), new FrameworkPropertyMetadata(typeof(WellImage)));
}
public RadialGradientBrush InnerBackGround
{
get { return (RadialGradientBrush)GetValue(InnerBackGroundProperty); }
set { SetValue(InnerBackGroundProperty, value); }
}
public RadialGradientBrush OuterBackGround
{
get { return (RadialGradientBrush)GetValue(OuterBackGroundProperty); }
set { SetValue(OuterBackGroundProperty, value); }
}
public SolidColorBrush WellBorder
{
get { return (SolidColorBrush)GetValue(WellBorderProperty); }
set { SetValue(WellBorderProperty, value); }
}
public double WellBorderThickness
{
get { return (double)GetValue(WellBorderThicknessProperty); }
set { SetValue(WellBorderThicknessProperty, value); }
}
}
}
and this is how tried to access this controll via c#
WellImage image = new WellImage();
image.Height = 40;
image.Width = 40;
image.Margin = new Thickness(30, 30, 30, 30);
image.VerticalAlignment = VerticalAlignment.Top;
image.HorizontalAlignment = HorizontalAlignment.Left;
image.Content = "WellButton";
grid.Children.Insert(0, image);
grid.Background = Brushes.LightBlue;
grid.Width = 120;
grid.Height = 100;
grid.VerticalAlignment = VerticalAlignment.Top;
grid.HorizontalAlignment = HorizontalAlignment.Left;
gridPartialedMicroPlate.Children.Insert(0, grid);
Why i fail to access my control?