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" />
我怎么能在我看来设置的绑定,而不是在风格的定义。这样我可以在多个视图中使用不同绑定的控件?
你可以用不同的对象设置'DataContext',并保持原样。或者您可以为您需要的每个绑定添加属性到您的SearchTextBox。 – kusi581
你能给我一个代码示例吗? – user2877820