2016-06-14 37 views
0

我派生了文本框類。在自定義文本框類中,我通過ImageSrc的名稱定義了依賴項屬性。這是customcontrol的代碼。WPF TemplatedBinding的圖像的依賴屬性

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> 

這裏是對於具有以顯示圖像的文本框風格。

<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> 

這裏是我已經使用的標籤,我想指定圖像。

<CustomControls:CustomTextBox Style="{StaticResource CustomTextBoxStyle}" Text="Test" WaterMark="By Participant" Margin="0,0,2,5" ImageSrc="ByParticipantSource" /> 

在綁定中存在一些我無法理解的問題。您的幫助將非常感激。

PS:ImageSrc實際上是一個包含圖像的關鍵字的字符串值。

+0

您已經聲明瞭attched屬性,而不是一個常規的依賴屬性。除此之外,您不能將Image控件的Source屬性設置爲資源鍵。 – Clemens

+0

@Clemens所以有沒有其他方式可以將圖像的資源鍵傳遞給樣式? –

回答

1

聲明一個普通的依賴屬性,而不是附加屬性,並改變其類型的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); } 
    } 
} 

然後分配這樣的資源:

<CustomControls:CustomTextBox ... ImageSrc="{StaticResource ByParticipantSource}" />