2013-12-16 43 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到模板文本 –