2013-12-16 176 views
1

我有一個名爲「txtAddress」的自定義文本框,其中包含ImageTextBox
我需要能夠訪問其內容:txtAddress.TexttxtAddress.Image
我綁定的inner textBox.textTemplate.Textinner rect.FillTemplate.Background(我用矩形現在,我將其更改爲圖像)WPF自定義文本框

enter image description here

當我運行該程序並編輯文本框一切看起來像它的工作和文本正在改變,但是當我使用它從代碼txtAddress.Text仍然是「我的電腦」這是初始值的TextBox。 我知道,因爲我沒有把<ContentPresenter>某處的風格,但在那裏我應該把控制(圖像,文本框),像ListView中有<DataTemplate>我們添加控件

TxtAddress風格:

<Style x:Key="TextBoxAddressStyle" BasedOn="{x:Null}" TargetType="{x:Type TextBox}"> 
     <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> 
     <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/> 
     <Setter Property="BorderBrush" Value="{StaticResource TextBoxBorder}"/> 
     <Setter Property="BorderThickness" Value="1"/> 
     <Setter Property="Padding" Value="1"/> 
     <Setter Property="AllowDrop" Value="true"/> 
     <Setter Property="FocusVisualStyle" Value="{x:Null}"/> 
     <Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/> 
     <Setter Property="Stylus.IsFlicksEnabled" Value="False"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type TextBox}"> 
        <Grid HorizontalAlignment="Stretch" Height="Auto" VerticalAlignment="Stretch" Width="Auto"> 
         <Rectangle Height="Auto" Stroke="{TemplateBinding Foreground}" VerticalAlignment="Stretch" RadiusY="11" RadiusX="11" Margin="0" Fill="{TemplateBinding OpacityMask}"/> 
         <!--<ContentPresenter Height="Auto" Margin="31,2,7.458,2"/>--> 
         <TextBox x:Name="TxtAddress" Height="Auto" Margin="31,2,7.458,2" VerticalAlignment="Stretch" MaxLines="1" Text="{TemplateBinding Text}" AcceptsTab="True" Style="{DynamicResource TextBoxStyle2}" SelectionBrush="#C859003D" FontSize="{TemplateBinding FontSize}" AcceptsReturn="False"/> 
         <Rectangle Fill="#5AFFFFFF" HorizontalAlignment="Stretch" Height="6.375" Margin="6,2.25,6,0" RadiusY="6" RadiusX="6" Stroke="{x:Null}" VerticalAlignment="Top" Width="Auto" d:IsLocked="True"/> 
         <Rectangle x:Name="ImgAddress" Fill="{TemplateBinding Background}" HorizontalAlignment="Left" Height="24" Margin="7,1,0,0" RadiusY="0" RadiusX="0" VerticalAlignment="Top" Width="24" StrokeThickness="0"> 
          <Rectangle.Stroke> 
           <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
            <GradientStop Color="Black" Offset="1"/> 
            <GradientStop Color="White"/> 
           </LinearGradientBrush> 
          </Rectangle.Stroke> 
         </Rectangle> 
        </Grid> 
        <ControlTemplate.Triggers> 
         <Trigger Property="IsEnabled" Value="false"> 
          <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> 
         </Trigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
     <Style.Triggers> 
      <MultiTrigger> 
       <MultiTrigger.Conditions> 
        <Condition Property="IsInactiveSelectionHighlightEnabled" Value="true"/> 
        <Condition Property="IsSelectionActive" Value="false"/> 
       </MultiTrigger.Conditions> 
       <Setter Property="SelectionBrush" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightBrushKey}}"/> 
      </MultiTrigger> 
     </Style.Triggers> 
    </Style> 

內文本框風格:

<Style x:Key="TextBoxStyle2" BasedOn="{x:Null}" TargetType="{x:Type TextBox}"> 
     <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> 
     <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/> 
     <Setter Property="BorderBrush" Value="{StaticResource TextBoxBorder}"/> 
     <Setter Property="BorderThickness" Value="1"/> 
     <Setter Property="Padding" Value="1"/> 
     <Setter Property="AllowDrop" Value="true"/> 
     <Setter Property="FocusVisualStyle" Value="{x:Null}"/> 
     <Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/> 
     <Setter Property="Stylus.IsFlicksEnabled" Value="False"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type TextBox}"> 
        <ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" FontSize="13.333" PanningMode="HorizontalOnly"/> 
        <ControlTemplate.Triggers> 
         <Trigger Property="IsEnabled" Value="false"> 
          <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> 
         </Trigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
     <Style.Triggers> 
      <MultiTrigger> 
       <MultiTrigger.Conditions> 
        <Condition Property="IsInactiveSelectionHighlightEnabled" Value="true"/> 
        <Condition Property="IsSelectionActive" Value="false"/> 
       </MultiTrigger.Conditions> 
       <Setter Property="SelectionBrush" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightBrushKey}}"/> 
      </MultiTrigger> 
     </Style.Triggers> 
    </Style> 

個TxtAddress:

<TextBox x:Name="txtAddress" Margin="34,5,32,0" TextWrapping="Wrap" Text="My Computer" Style="{DynamicResource TextBoxAddressStyle}" Height="25" VerticalAlignment="Top" FontWeight="Bold" MinHeight="25" MaxHeight="25"> 
      <TextBox.Background> 
       <ImageBrush ImageSource="BtnImg/computer.png" Stretch="None"/> 
      </TextBox.Background> 
      <TextBox.Foreground> 
       <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
        <GradientStop Color="#FF779198" Offset="0.1"/> 
        <GradientStop Color="#FF789399" Offset="0.93"/> 
        <GradientStop Color="#FFBFD3D7" Offset="0.513"/> 
        <GradientStop Color="#FF343E41" Offset="1"/> 
        <GradientStop Color="#FF5C6E73"/> 
       </LinearGradientBrush> 
      </TextBox.Foreground> 
      <TextBox.OpacityMask> 
       <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
        <GradientStop Color="#FF94B5BD" Offset="0.1"/> 
        <GradientStop Color="#FF94B5BD" Offset="0.93"/> 
        <GradientStop Color="#FFE7FBFF" Offset="0.513"/> 
        <GradientStop Color="#FF7B9399" Offset="1"/> 
        <GradientStop Color="#FF7B9399"/> 
       </LinearGradientBrush> 
      </TextBox.OpacityMask> 
     </TextBox> 

在此先感謝。

+1

你爲什麼要在代碼中使用它?你在使用MVVM嗎?綁定到您的ViewModel是否正常工作? – nvoigt

+0

綁定工作正確的「一半」,我的意思是當我運行該項目的內部文本在圖形不改變其內容,所以如果我改變文字爲「無論」,當我從代碼使用它仍然「我的電腦」 –

回答

0

您正在將TextBox放置在模板中以供其他TextBox使用。

視覺樹看起來就像這樣:

TextBox 
    Grid 
     Rectangle 
     TextBox 
     Rectangle 
     Rectangle 

這是不是在首位的理想,但如果你想保持這樣的事情,只要確保你內在的文本框的Text屬性綁定到外一個,通過Two WayUpdateSourceTrigger=PropertyChanged數據綁定像這樣:

<TextBox x:Name="TxtAddress" Text="{Binding Text, 
              Mode=TwoWay, 
              UpdateSourceTrigger=PropertyChanged, 
              RelativeSource={RelativeSource AncestorType={x:Type TextBox}}}" ... /> 

除了這一切,我已經提到的幾個牛逼imes,你不應該操縱WPF程序代碼中的UI元素。

相反,建立一個適當的視圖模型來存儲你的數據:

public class MyViewModel 
{ 
    public string Text {get;set;} 
} 

然後使用數據綁定到你的文本框綁定到數據:

<TextBox Text="{Binding Text}"/> 

,每當你需要檢索值,檢索來自VM的值,而不是UI。

+0

你認爲這個textBox的理想之處是什麼?我應該創建包含圖像和文本框的用戶控件? –

+0

@MurHaf或者,或者從[Default one](http://msdn.microsoft.com/en-us/library/cc645061(v = vs.95).aspx)開始創建您的TextBox模板,它已經包含相關的[模板部分](http://paulstovell.com/blog/wpf-part-names) –

+0

,我做了什麼,我編輯一個副本,然後我刪除了鉻然後我添加了控件,然後我綁定innerText到模板文本 –