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. WPF
  4. Sivlerlight: the best alternative to a simple WPF-like DataTrigger?

Sivlerlight: the best alternative to a simple WPF-like DataTrigger?

Scheduled Pinned Locked Moved WPF
wpfquestioncsharpwcf
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.
  • M Offline
    M Offline
    Member 1033907
    wrote on last edited by
    #1

    Hi, I have a ListBox bond to a collection of following objects with string and bool property:

    class Item
    {
    public string Text {get;set;}
    public bool IsBold {get;set;}
    }

    The DataTemplate is simple enough:

    <DataTemplate>
    <TextBlock Text="{Binding Text}" />
    </DataTemplate>

    (could actually use DisplayMemberPath instead, I know) Now, I want those items that have IsBold == true to be bold-faced. As in WPF, where you add a simple DataTrigger that sets FontWeight=Bold on the TextBlock if IsBold is true. Is something like this possible in Silverlight and what is the recommended approach? Thanks, H.

    P 1 Reply Last reply
    0
    • M Member 1033907

      Hi, I have a ListBox bond to a collection of following objects with string and bool property:

      class Item
      {
      public string Text {get;set;}
      public bool IsBold {get;set;}
      }

      The DataTemplate is simple enough:

      <DataTemplate>
      <TextBlock Text="{Binding Text}" />
      </DataTemplate>

      (could actually use DisplayMemberPath instead, I know) Now, I want those items that have IsBold == true to be bold-faced. As in WPF, where you add a simple DataTrigger that sets FontWeight=Bold on the TextBlock if IsBold is true. Is something like this possible in Silverlight and what is the recommended approach? Thanks, H.

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      You could always bind a DataTrigger inside a style to do this, so you would end up with something like this:

      <TextBlock Text="{Binding Text}">
      <TextBlock.Style>
      <Style TargetType="{x:Type TextBox}">
      <Style.Triggers>
      <DataTrigger Binding="{Binding Path=Text}" Value="True">
      <Setter Property="FontWeight" Value="Bold" />
      </DataTrigger>
      </Style.Triggers>
      </Style>
      </TextBlock.Style>
      </TextBlock>

      Note that I just typed this up in the browser window, so the syntax may need a minor tweak.

      Forgive your enemies - it messes with their heads

      "Mind bleach! Send me mind bleach!" - Nagy Vilmos

      My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

      M 1 Reply Last reply
      0
      • P Pete OHanlon

        You could always bind a DataTrigger inside a style to do this, so you would end up with something like this:

        <TextBlock Text="{Binding Text}">
        <TextBlock.Style>
        <Style TargetType="{x:Type TextBox}">
        <Style.Triggers>
        <DataTrigger Binding="{Binding Path=Text}" Value="True">
        <Setter Property="FontWeight" Value="Bold" />
        </DataTrigger>
        </Style.Triggers>
        </Style>
        </TextBlock.Style>
        </TextBlock>

        Note that I just typed this up in the browser window, so the syntax may need a minor tweak.

        Forgive your enemies - it messes with their heads

        "Mind bleach! Send me mind bleach!" - Nagy Vilmos

        My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

        M Offline
        M Offline
        Member 1033907
        wrote on last edited by
        #3

        Would have been cool is Silverlight supported triggers at all, there is no Style.Triggers collection... :( I'll do some digging, but honestly I didn't expect I'll spend so much time on such a simple matter you just take for granted in WPF.

        P 1 Reply Last reply
        0
        • M Member 1033907

          Would have been cool is Silverlight supported triggers at all, there is no Style.Triggers collection... :( I'll do some digging, but honestly I didn't expect I'll spend so much time on such a simple matter you just take for granted in WPF.

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          I know it's an inelegant hack, but you could always use a value converter here.

          Forgive your enemies - it messes with their heads

          "Mind bleach! Send me mind bleach!" - Nagy Vilmos

          My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

          M 1 Reply Last reply
          0
          • P Pete OHanlon

            I know it's an inelegant hack, but you could always use a value converter here.

            Forgive your enemies - it messes with their heads

            "Mind bleach! Send me mind bleach!" - Nagy Vilmos

            My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

            M Offline
            M Offline
            Member 1033907
            wrote on last edited by
            #5

            That is what I used for the time being - BooleanToFontWeightConverter, nice:-) - but I am looking for a more systematic approach because I am sure that design requirements will get more complicated than FontWeight=Bold.

            P 1 Reply Last reply
            0
            • M Member 1033907

              That is what I used for the time being - BooleanToFontWeightConverter, nice:-) - but I am looking for a more systematic approach because I am sure that design requirements will get more complicated than FontWeight=Bold.

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              Potentially, you could wrap things into a nice Blend attached behavior. I've done this to get round SL deficiencies in the past.

              Forgive your enemies - it messes with their heads

              "Mind bleach! Send me mind bleach!" - Nagy Vilmos

              My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

              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