2017-01-11 41 views
0

我想讓這樣的事情:創建類似文本與圖標/圖像包含

enter image description here

所以它看起來像文本框,它能夠在中心寫所以它是文本框acctualy,但左側有放大鏡圖標,右側有鍵盤圖標,但看起來就像是它的全部控制!

所以,大家好,誰能幫我創建這個?

它是錯的,如果我做的下一件事:

enter image description here

或者更正確與3列上創建網格?

第一列將包含放大鏡圖標

第二列將包含文本

第三列將包含鍵盤圖標

或者還有的是我不知道的第三條道路而且這比我建議的這兩種解決方案更好。

非常感謝! 此社區是最好的!

+1

看看這個問題http://stackoverflow.com/questions/13927364/wpf-textbox-with-image – taquion

+1

我發現這篇文章可以幫助您。 https://davidowens.wordpress.com/2009/02/18/wpf-search-text-box/ – taquion

回答

0

或者用3列創建網格更正確嗎?

是的,如果中間的文本框沒有固定的寬度,因爲StackPanel不會測量其子元素,這會更好。

你可以有3列添加一個網格,一個用戶控件並綁定元素添加到用戶控件的依賴屬性的屬性:

UserControl1.xaml:

<UserControl x:Class="WpfApplication1.UserControl1" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:WpfApplication1" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300" 
      x:Name="uc"> 
    <Grid> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="Auto" /> 
      <ColumnDefinition Width="*" /> 
      <ColumnDefinition Width="Auto" /> 
     </Grid.ColumnDefinitions> 
     <Image Grid.Column="0" Width="20" Height="20" /> 
     <TextBox Text="{Binding Text, ElementName=uc}" /> 
     <Image Grid.Column="1" Source="{Binding ImageSource, ElementName=uc}" Width="20" Height="20" /> 
    </Grid> 
</UserControl> 

UserControl1.xaml .cs:

public partial class UserControl1 : UserControl 
{ 
    public UserControl1() 
    { 
     InitializeComponent(); 
    } 

    public static readonly DependencyProperty TextProperty = 
     DependencyProperty.Register("Text", typeof(string), typeof(UserControl4)); 

    public string Text 
    { 
     get { return (string)GetValue(TextProperty); } 
     set { SetValue(TextProperty, value); } 
    } 

    public static readonly DependencyProperty ImageSourceProperty = 
     DependencyProperty.Register("ImageSource", typeof(Uri), typeof(UserControl4)); 

    public Uri ImageSource 
    { 
     get { return (Uri)GetValue(ImageSourceProperty); } 
     set { SetValue(ImageSourceProperty, value); } 
    } 
} 

用法:

<local:UserControl1 Text="..." ImageSource="Images/screen.png" /> 
+0

你試過這個或發生了什麼? – mm8

相關問題