2017-07-27 100 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" /> 

我怎么能在我看来设置的绑定,而不是在风格的定义。这样我可以在多个视图中使用不同绑定的控件?

+0

你可以用不同的对象设置'DataContext',并保持原样。或者您可以为您需要的每个绑定添加属性到您的SearchTextBox。 – kusi581

+0

你能给我一个代码示例吗? – user2877820

回答

0

风格自定义文本框:

 <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> 

的SearchTextBox.cs的代码:

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; } 
} 

,因为该命令清除文本框始终是一样的,SearchTextBox包含命令进行结算它。

现在可以在View中使用SearchTextBox,并设置SearchCommand属性。

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

正如你可以在样式为SearchTextBox看到,该文本被设置为在SearchCommand参数,所以你可以使用它,指定的命令是在哪里。