2012-03-07 5 views
1

をoveridingせずに、私は以下のコードでテンプレートをオーバーライドするテキストボックスにボタンを実装し、テキストボックスの機能のボタンを与える:は、カスタムコントロールを使用しますが、テキストボックスのテンプレート

<Style x:Key="{x:Type TextBox}" TargetType="{x:Type TextBoxBase}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type TextBoxBase}"> 

       <Border Name="Border" 
        CornerRadius="2" 
        Padding="2" 
        Background="{StaticResource WindowBackgroundBrush}" 
        BorderBrush="{StaticResource SolidBorderBrush}" 
        BorderThickness="1"> 

        <Grid x:Name="LayoutGrid"> 
         <Grid.ColumnDefinitions> 
          <ColumnDefinition Width="*" /> 
          <ColumnDefinition Width="Auto" /> 
          <ColumnDefinition Width="Auto" /> 
         </Grid.ColumnDefinitions> 

         <ScrollViewer x:Name="PART_ContentHost" Grid.Column="0" /> 


         <Button x:Name="XButton" 
           Grid.Column="2" 
           Width="25" 
           Height="25" 
           Content="X" 
           Visibility="Visible" 
           BorderBrush="Transparent" 
           Command="{Binding ButtonClick}" /> 
        </Grid> 

       </Border> 

      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

ボタンはすべてのコンテンツを削除する必要がありますテキストボックスにヒットするとこれについては、コマンド "Command =" {Binding ButtonClick} ""

を使用したいと思いますか?カスタムコントロールとライブラリを作成せずにバインドできますか? またはバインディングや機能をどのように行うことができますか?

私はMVVMパターンを使用しています。たとえば、ViewModelを使用して、それをバインドするプロパティを作成する方法がありますか?そのようなスタイルでのバインディングのために

答えて

1

は、私は私が上のクリックを検出するスタイルでアイテムのクリックを検出するためにそれを使用AttachedCommandBehaviors「ACB」

http://marlongrech.wordpress.com/2008/12/13/attachedcommandbehavior-v2-aka-acb/

を使用しています。私はボタンのためにそれを使用していないが、私はそれだけでもうまくいくと思うだろう。

私は、ユーザーコントロールであるスタイルの祖先にバインドしていますが、これはアプリケーションのために変更する必要があります。

xamns:acb名前空間をxamlに追加する必要があります。詳しくはリンクをご覧ください。

<ControlTemplate TargetType="{x:Type ListViewItem}"> 
        <StackPanel x:Name="IconBorder" 
           acb:CommandBehavior.Event="PreviewMouseUp" 
           acb:CommandBehavior.Command="{Binding Path=DataContext.SelectedListItemSingleClickCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}" 
           acb:CommandBehavior.CommandParameter="{Binding}"> 
         <DockPanel LastChildFill="False" HorizontalAlignment="Left" Width="{Binding Width, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListViewItem}}}"> 
          <TextBlock Text="{Binding Title}" 
             DockPanel.Dock="Left" 
             x:Name="ListItemText" /> 
          <Image x:Name="ActionIcon" 
            Source="{Binding Path=DataContext.SelectedListActionIcon, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}" 
            DockPanel.Dock="Right" 
            acb:CommandBehavior.Event="PreviewMouseUp" 
            acb:CommandBehavior.Command="{Binding Path=DataContext.SelectedListActionIconClickCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}" 
            acb:CommandBehavior.CommandParameter="{Binding}"> 
          </Image> 
         </DockPanel> 
         <ContentPresenter DockPanel.Dock="Left" /> 
        </StackPanel> 
</ControlTemplate> 
関連する問題