2017-07-27 11 views
0

SearchTextBoxのカスタムスタイルがあります。私はこのコントロールに複数のバインディングを持っています。WPF - カスタムスタイルの子コントロールのプロパティを設定する

<Style TargetType="{x:Type controls:SearchTextBox}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type controls:SearchTextBox}"> 
       <Grid> 
        <TextBox Text="{Binding SearchText, UpdateSourceTrigger=PropertyChanged}"> 
         <TextBox.InputBindings> 
          <KeyBinding Command="{Binding Path=SearchCommand}" Key="Enter" /> 
          <KeyBinding Command="{Binding Path=DeleteSearchCommand}" Key="Esc" /> 
         </TextBox.InputBindings> 
        </TextBox> 
        <Button Style="{StaticResource WatermarkButtonCancelStyle}" HorizontalAlignment="Right" Command="{Binding DeleteSearchCommand}" Margin="0,0,22,0"/> 
        <Button Style="{StaticResource WatermarkButtonSearchStyle}" HorizontalAlignment="Right" Command="{Binding SearchCommand}"/> 
       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

私は右ここに私の見解でテキストボックスを使用しています:

<controls:SearchTextBox Width="300" HorizontalAlignment="Left" Margin="0,0,0,6" /> 

を、私は私の見解ではなく、スタイルの定義でバインディングを設定するにはどうすればよいです。私は異なるバインディングで複数のビューでコントロールを使用することができますか?カスタムTextBoxのため

+0

異なるオブジェクトを持つ 'DataContext'を設定し、そのままにしておくことができます。または、必要なバインディングごとにSearchTextBoxにプロパティを追加することもできます。 – kusi581

+0

私にコードの例を教えてもらえますか? – user2877820

答えて

0

スタイル:SearchTextBox.csの

 <Style TargetType="{x:Type local:SearchTextBox}"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type local:SearchTextBox}"> 
        <DockPanel DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:SearchTextBox}}}"> 
         <Button DockPanel.Dock="Right" 
           Style="{StaticResource WatermarkButtonCancelStyle}" 
           Command="{Binding DeleteSearchCommand}"/> 
         <Button DockPanel.Dock="Right" 
           Style="{StaticResource WatermarkButtonSearchStyle}" 
           Command="{Binding SearchCommand}" 
           CommandParameter="{Binding Text}"/> 
         <TextBox x:Name="InnerTextBox" 
           Text="{Binding Path=Text}"> 
          <TextBox.InputBindings> 
           <KeyBinding Command="{Binding SearchCommand}" 
              CommandParameter="{Binding Text}" 
              Key="Enter" /> 
           <KeyBinding Command="{Binding DeleteSearchCommand}" 
              Key="Escape" /> 
          </TextBox.InputBindings> 
         </TextBox> 
        </DockPanel> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

コード:

public class SearchTextBox : TextBox 
{ 
    public static readonly DependencyProperty SearchCommandProperty = DependencyProperty.Register(
     "SearchCommand", typeof(ICommand), typeof(SearchTextBox), new PropertyMetadata(default(ICommand))); 

    public SearchTextBox() 
    { 
     DeleteSearchCommand = new Command 
     { 
      ExecuteHandler = o => Clear() 
     }; 
    } 

    public ICommand SearchCommand 
    { 
     get { return (ICommand) GetValue(SearchCommandProperty); } 
     set { SetValue(SearchCommandProperty, value); } 
    } 

    public Command DeleteSearchCommand { get; private set; } 
} 

のTextBoxをクリアするためのコマンドが常に同じであるため、SearchTextBoxはクリアするためのコマンドが含まれていますそれ。

これで、SearchTextBoxをビューで使用し、SearchCommandプロパティを設定することができます。

<local:SearchTextBox Height="30" Width="200" SearchCommand="{Binding DoTheSearch}"/> 

あなたがSearchTextBoxのスタイルで見ることができるように、テキストがSearchCommandのパラメータとして設定されているので、コマンドは指定された場合、あなたは、それを使用することができます。

関連する問題