我有以下问题:我在配置文件中有一个布尔变量。如果确实如此,我希望根据该变量的值设置文本框控件中的属性。 尝试上面的解决方案,但它不起作用。我究竟做错了什么? 这是一个片段代码:
bool isKeyboardAvtive = true; //read from configuration file
<Style x:Key="StylesTextBox" TargetType="{x:Type TextBox}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=isKeyboardActive}" Value="True">
<Setter Property="k:TouchScreenKeyboard.TouchScreenKeyboard" Value="True"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding Path=isKyboardActive}" Value="False">
<Setter Property="k:TouchScreenKeyboard.TouchScreenKeyboard" Value="False"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
<TextBox Style="{StaticResource StylesTextBox}" Margin="0,5" x:Name="txtUserName" Height="40" Width="150" />
答案 0 :(得分:1)
IsKeyboardActive
需要是DataContext
的公共属性才能使绑定生效。此外,您不需要触发器,只需要绑定:
k:TouchScreenKeyboard.TouchScreenKeyboard="{Binding IsKeyBoardActive}"
如果您使用标准的VS生成设置,您还可以直接绑定到设置:
xmlns:prop="clr-namespace:YourApplication.Properties"
...
k:TouchScreenKeyboard.TouchScreenKeyboard="{Binding IsKeyBoardActive, Source={x:Static prop:Settings.Default}}"
甚至更好,使用this markup extension:
xmlns:local="clr-namespace:YourApplication"
...
k:TouchScreenKeyboard.TouchScreenKeyboard="{local:SettingBinding IsKeyBoardActive}"