更好(更简单)的方法是将样式定义为可以轻松应用于任何TextBox的资源:
- <Window.Resources>
- <c:MyLogicConverter x:Key="LogicConverter" />
- <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}" x:Key="MultiBound">
- <Setter Property="IsEnabled">
- <Setter.Value>
- <MultiBinding Converter="{StaticResource LogicConverter}">
- <Binding ElementName="switch" Path="IsEnabled" />
- <Binding ElementName="switch" Path="IsChecked" />
- </MultiBinding>
- </Setter.Value>
- </Setter>
- </Style>
- </Window.Resources>
- <StackPanel Orientation="Horizontal">
- <CheckBox Name="switch" />
- <TextBox Name="textBox2" Text="Test" Style="{StaticResource MultiBound}" />
- </StackPanel>