Reputation: 1209
I have derived the textbox class. In custom textbox class i have defined dependency property by the name of ImageSrc. Here is the code for customcontrol.
CustomControl.cs
public class CustomTextBox : TextBox
{
public static string GetImageSrc(DependencyObject obj)
{
return (string)obj.GetValue(ImageSrcProperty);
}
public static void SetImageSrc(DependencyObject obj, string value)
{
obj.SetValue(ImageSrcProperty, value);
}
public static readonly DependencyProperty ImageSrcProperty =
DependencyProperty.RegisterAttached("ImageSrc", typeof(string), typeof(CustomTextBox ), new PropertyMetadata(""));
}
MainWindow.xaml
<Window.Resources>
<DrawingImage x:Key="ByParticipantSource">
<DrawingImage.Drawing>
<GeometryDrawing Brush="White"
Geometry="M12.555,10.734h-0.91c-1.007,0-1.822-0.815-1.822-1.824V8.325
c0.402-0.478,0.691-1.046,0.871-1.644c0.019-0.101,0.117-0.151,0.182-0.221c0.349-0.349,0.417-0.938,0.156-1.356
c-0.037-0.064-0.101-0.119-0.097-0.198c0-0.534,0.002-1.07-0.002-1.604c-0.013-0.645-0.198-1.315-0.65-1.792
C9.919,1.125,9.416,0.895,8.901,0.797C8.247,0.672,7.564,0.679,6.917,0.844C6.355,0.984,5.829,1.313,5.503,1.8
C5.216,2.223,5.089,2.737,5.067,3.243C5.06,3.787,5.065,4.332,5.063,4.876c0.013,0.109-0.08,0.183-0.122,0.273
C4.697,5.597,4.804,6.207,5.201,6.532c0.1,0.07,0.119,0.196,0.156,0.304c0.172,0.539,0.458,1.036,0.821,1.47V8.91
c0,1.009-0.815,1.824-1.822,1.824H3.443c0,0-1.652,0.456-2.732,2.732v0.912c0,0.504,0.406,0.91,0.91,0.91h12.756
c0.504,0,0.912-0.406,0.912-0.91v-0.912C14.206,11.19,12.555,10.734,12.555,10.734z" />
</DrawingImage.Drawing>
</DrawingImage>
</Window.Resources>
Here is the Style for the textbox which have to show the image.
<Style x:Key="CustomTextBoxStyle" BasedOn="{x:Null}" TargetType="{x:Type CustomControl:CustomTextBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type CustomControl:CustomTextBox}">
<Themes:ListBoxChrome x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderFocused="{TemplateBinding IsKeyboardFocusWithin}" SnapsToDevicePixels="true" >
<Grid Margin="5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Image Width="16" Height="16" Source="{TemplateBinding ImageSrc}" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Column="0" Margin="0,0,5,0" />
<ScrollViewer x:Name="PART_ContentHost" VerticalAlignment="Stretch" VerticalContentAlignment="Center" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Grid.Column="1" Template="{DynamicResource HDADMTBScrollViewerControlTemplate}"/>
</Grid>
</Themes:ListBoxChrome>
</Setter.Value>
</Setter>
</Style>
Here is the tag that i have used where i want to specify the image.
<CustomControls:CustomTextBox Style="{StaticResource CustomTextBoxStyle}" Text="Test" WaterMark="By Participant" Margin="0,0,2,5" ImageSrc="ByParticipantSource" />
There is some issue in binding which i am unable to understand. Your help will be really appreciated.
PS: ImageSrc is actully a string value which contains the key of image.
Upvotes: 0
Views: 207
Reputation: 128060
Declare a regular dependency property instead of an attached property, and change its type to ImageSource:
public class CustomTextBox : TextBox
{
public static readonly DependencyProperty ImageSrcProperty =
DependencyProperty.Register(
"ImageSrc", typeof(ImageSource), typeof(CustomTextBox));
public ImageSource ImageSrc
{
get { return (ImageSource)GetValue(ImageSrcProperty); }
set { SetValue(ImageSrcProperty, value); }
}
}
Then assign the resource like this:
<CustomControls:CustomTextBox ... ImageSrc="{StaticResource ByParticipantSource}" />
Upvotes: 1