Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WCF and WF
  4. Checkbox trigger conditions binding

Checkbox trigger conditions binding

Scheduled Pinned Locked Moved WCF and WF
wpfquestiondatabasewcfarchitecture
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sevententh
    wrote on last edited by
    #1

    How can I bind a checkbox IsChecked value to a condition from a combobox?

    <ComboBox x:Name="airport"
    ItemsSource="{Binding Path=AirportList}"
    SelectedValuePath="AirportID"
    SelectedValue="{Binding Path=CurrentItem.AirportID}"/>
    <CheckBox Content="International" IsChecked="{Binding Path=CurrentItem.International}">
    <CheckBox.Template>
    <ControlTemplate TargetType="CheckBox">
    <ControlTemplate.Triggers>
    <MultiTrigger>
    <MultiTrigger.Conditions>
    <Condition Binding="{Binding ElementName=airport, Path=SelectedItem.CountryID}" Value="{Binding Path=CurrentItem.ThisCountryID}" />
    </MultiTrigger.Conditions>
    <Setter Property="CheckBox.IsChecked" Value="True" />
    </MultiTrigger>
    </ControlTemplate.Triggers>
    </ControlTemplate>
    </CheckBox.Template>
    </CheckBox>

    I'm using MVVM, and the VM has

    public AirportDestination CurrentItem = new AirportDestination() //This record - ID, ThisCountryID, AirportID, International
    public ObservableCollection<LikelyDestinations> AirportList = new ObservableCollection<LikelyDestinations>();//Airport Listing - AirportID, CountryID, Name

    The problem is the Condition value binding. You can't! I want to change the IsChecked based on the condition that the selected combobox SelectedItem.CountryID equals ThisCountryID and also hold the value in CurrentItem.International Anybody have a clue how to do this in XAML only!

    I 1 Reply Last reply
    0
    • S Sevententh

      How can I bind a checkbox IsChecked value to a condition from a combobox?

      <ComboBox x:Name="airport"
      ItemsSource="{Binding Path=AirportList}"
      SelectedValuePath="AirportID"
      SelectedValue="{Binding Path=CurrentItem.AirportID}"/>
      <CheckBox Content="International" IsChecked="{Binding Path=CurrentItem.International}">
      <CheckBox.Template>
      <ControlTemplate TargetType="CheckBox">
      <ControlTemplate.Triggers>
      <MultiTrigger>
      <MultiTrigger.Conditions>
      <Condition Binding="{Binding ElementName=airport, Path=SelectedItem.CountryID}" Value="{Binding Path=CurrentItem.ThisCountryID}" />
      </MultiTrigger.Conditions>
      <Setter Property="CheckBox.IsChecked" Value="True" />
      </MultiTrigger>
      </ControlTemplate.Triggers>
      </ControlTemplate>
      </CheckBox.Template>
      </CheckBox>

      I'm using MVVM, and the VM has

      public AirportDestination CurrentItem = new AirportDestination() //This record - ID, ThisCountryID, AirportID, International
      public ObservableCollection<LikelyDestinations> AirportList = new ObservableCollection<LikelyDestinations>();//Airport Listing - AirportID, CountryID, Name

      The problem is the Condition value binding. You can't! I want to change the IsChecked based on the condition that the selected combobox SelectedItem.CountryID equals ThisCountryID and also hold the value in CurrentItem.International Anybody have a clue how to do this in XAML only!

      I Offline
      I Offline
      Ian Shlasko
      wrote on last edited by
      #2

      In XAML only, I don't think you can (Would love to be proven wrong), but you could keep it generic with a MultiBinding...

      <MultiBinding Converter="{StaticResource EqualityConverter}">
      <Binding ElementName="airport" Path="SelectedItem.CountryID"/>
      <Binding Path="CurrentItem.CountryID"/>
      </MultiBinding>

      You have to make the EqualityConverter (Or whatever you want to name it) in the code somewhere... Just make a class that inherits IMultiValueConverter and returns true only if the two values are equal.

      Proud to have finally moved to the A-Ark. Which one are you in? Author of Guardians of Xen (Sci-Fi/Fantasy novel)

      S 1 Reply Last reply
      0
      • I Ian Shlasko

        In XAML only, I don't think you can (Would love to be proven wrong), but you could keep it generic with a MultiBinding...

        <MultiBinding Converter="{StaticResource EqualityConverter}">
        <Binding ElementName="airport" Path="SelectedItem.CountryID"/>
        <Binding Path="CurrentItem.CountryID"/>
        </MultiBinding>

        You have to make the EqualityConverter (Or whatever you want to name it) in the code somewhere... Just make a class that inherits IMultiValueConverter and returns true only if the two values are equal.

        Proud to have finally moved to the A-Ark. Which one are you in? Author of Guardians of Xen (Sci-Fi/Fantasy novel)

        S Offline
        S Offline
        Sevententh
        wrote on last edited by
        #3

        Thanks for the update. It's kind of working :confused: When it updates, it changes the Content to either True or False but does not actually check the box??? I don't what the Content to change just the IsChecked value... any clues?

        I 1 Reply Last reply
        0
        • S Sevententh

          Thanks for the update. It's kind of working :confused: When it updates, it changes the Content to either True or False but does not actually check the box??? I don't what the Content to change just the IsChecked value... any clues?

          I Offline
          I Offline
          Ian Shlasko
          wrote on last edited by
          #4

          Well it looks like you've already bound the IsChecked property. Try binding that to this MultiBinding instead of making a whole template for it.

          Proud to have finally moved to the A-Ark. Which one are you in? Author of Guardians of Xen (Sci-Fi/Fantasy novel)

          S 1 Reply Last reply
          0
          • I Ian Shlasko

            Well it looks like you've already bound the IsChecked property. Try binding that to this MultiBinding instead of making a whole template for it.

            Proud to have finally moved to the A-Ark. Which one are you in? Author of Guardians of Xen (Sci-Fi/Fantasy novel)

            S Offline
            S Offline
            Sevententh
            wrote on last edited by
            #5

            This updates the Checkbox nicely. The values SelectedItem.CountryID and CurrentItem.ThisCountryID are compared to each other via the converter

            <CheckBox FontSize="12" HorizontalAlignment="Left" VerticalAlignment="Center">
            <CheckBox.IsChecked>
            <MultiBinding Converter="{StaticResource BooleanConverter}">
            <Binding ElementName="airport" Path="SelectedItem.CountryID" />
            <Binding Path="CurrentItem.ThisCountryID" />
            <Binding Path="CurrentItem.International" Mode="OneWayToSource" />
            </MultiBinding>
            </CheckBox.IsChecked>
            </CheckBox>

            However, I can't get the final binding to bind, CurrentItem.International is the field stored in db

            I 1 Reply Last reply
            0
            • S Sevententh

              This updates the Checkbox nicely. The values SelectedItem.CountryID and CurrentItem.ThisCountryID are compared to each other via the converter

              <CheckBox FontSize="12" HorizontalAlignment="Left" VerticalAlignment="Center">
              <CheckBox.IsChecked>
              <MultiBinding Converter="{StaticResource BooleanConverter}">
              <Binding ElementName="airport" Path="SelectedItem.CountryID" />
              <Binding Path="CurrentItem.ThisCountryID" />
              <Binding Path="CurrentItem.International" Mode="OneWayToSource" />
              </MultiBinding>
              </CheckBox.IsChecked>
              </CheckBox>

              However, I can't get the final binding to bind, CurrentItem.International is the field stored in db

              I Offline
              I Offline
              Ian Shlasko
              wrote on last edited by
              #6

              The multibinding can't go in two directions... A multibinding combines two or more bindings to set one value... It doesn't do OneWayToSource.

              Proud to have finally moved to the A-Ark. Which one are you in? Author of Guardians of Xen (Sci-Fi/Fantasy novel)

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups