tags:

views:

33

answers:

2

I have the following question: I have a Boolean variable in a configuration file. If it is true I want a property in textbox control to be setup according to the value of that variable. Try the solution above but it does not work. What am I doing wrong? This is a fragment code:

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" />
+1  A: 

IsKeyboardActive needs to be a public property of the DataContext for the binding to work. Also, you don't need a trigger there, just a binding :

k:TouchScreenKeyboard.TouchScreenKeyboard="{Binding IsKeyBoardActive}"

If you use the standard VS-generated settings, you can also bind to the settings directly :

xmlns:prop="clr-namespace:YourApplication.Properties"
...

k:TouchScreenKeyboard.TouchScreenKeyboard="{Binding IsKeyBoardActive, Source={x:Static prop:Settings.Default}}"

Or even better, using this markup extension :

xmlns:local="clr-namespace:YourApplication"
...

k:TouchScreenKeyboard.TouchScreenKeyboard="{local:SettingBinding IsKeyBoardActive}"
Thomas Levesque
A: 

Hi Thomas, Thank you very much foy your response. I downloaded your sample and it work very well, however I intended the same in my application but did not work. I dont now why. could you help me plase?

Configuration file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
      <section name="CashRegister.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
    </sectionGroup>
  </configSections>
  <userSettings>
    <CashRegister.Properties.Settings>
      <setting name="isKeyboardActive" serializeAs="bool">
        <value>true</value>
      </setting>
    </CashRegister.Properties.Settings>
  </userSettings>
</configuration>

(HERE I TRIED WITH serializeAs="bool" and "String")

In XAML:

xmlns:local="clr-namespace:CashRegister" 
...
<TextBox k:TouchScreenKeyboard.TouchScreenKeyboard="{local:SettingBinding isKeyboardActive}" Name="txtCustNumber" Text="{Binding Path=CustID}" ></TextBox>

And I defined your class SettingBindingExtension.

Carlos