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. objects added to an IDictionary must have a Key attribute [modified]

objects added to an IDictionary must have a Key attribute [modified]

Scheduled Pinned Locked Moved WPF
helpquestionwpfwcf
6 Posts 3 Posters 5 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.
  • L Offline
    L Offline
    Lutoslaw
    wrote on last edited by
    #1

    Hello again! It is another time I encounter this error: "objects added to an IDictionary must have a Key attribute or some other type of key associated with them.". I searched google to find out what is going on, but in every case the reason was that somebdy didn't set either Key or TargetType. I my case, I set the TargetType, but it doesn't work. In my previous post the problem was that I was using a UserControl. Now I try to set a template of a ToggleButton which is not a UserControl. WTH is going on? :confused:

    (...)

    The error started to appear since I changed something. I had tried to revert my recent changes but I couln't work out what is wrong :doh:
    EDIT: I've tried to set x:Key and reference Template={StRes key} and got exception:

    {"'Set property 'System.Windows.DataTrigger.Binding' threw an exception.' Line number '59' and line position '18'."}

    Inner:

    {"Unable to cast object of type 'System.Windows.TemplateBindingExpression' to type 'System.Windows.Data.BindingBase'."}

    Inner:null
    Edit2: IsChecked IS a dep property
    Any ideas?

    Greetings - Jacek

    modified on Monday, May 16, 2011 5:46 AM

    L V 2 Replies Last reply
    0
    • L Lutoslaw

      Hello again! It is another time I encounter this error: "objects added to an IDictionary must have a Key attribute or some other type of key associated with them.". I searched google to find out what is going on, but in every case the reason was that somebdy didn't set either Key or TargetType. I my case, I set the TargetType, but it doesn't work. In my previous post the problem was that I was using a UserControl. Now I try to set a template of a ToggleButton which is not a UserControl. WTH is going on? :confused:

      (...)

      The error started to appear since I changed something. I had tried to revert my recent changes but I couln't work out what is wrong :doh:
      EDIT: I've tried to set x:Key and reference Template={StRes key} and got exception:

      {"'Set property 'System.Windows.DataTrigger.Binding' threw an exception.' Line number '59' and line position '18'."}

      Inner:

      {"Unable to cast object of type 'System.Windows.TemplateBindingExpression' to type 'System.Windows.Data.BindingBase'."}

      Inner:null
      Edit2: IsChecked IS a dep property
      Any ideas?

      Greetings - Jacek

      modified on Monday, May 16, 2011 5:46 AM

      L Offline
      L Offline
      Lutoslaw
      wrote on last edited by
      #2

      This works:

          <Style TargetType="ToggleButton">
              <Setter Property="Template">
                  <Setter.Value>
                      <ControlTemplate TargetType="ToggleButton">
                          <Image MouseDown="Image\_MouseDown" Source="{StaticResource CheckedImage}" Height="16" />
                      </ControlTemplate>
                  </Setter.Value>                
              </Setter>
              <Style.Triggers>
                  <DataTrigger Binding="{Binding Path=IsChecked, RelativeSource={RelativeSource Mode=TemplatedParent}}" Value="True">
                      <Setter Property="Opacity" Value="1" />
                  </DataTrigger>
                  <DataTrigger Binding="{Binding Path=IsMoueseOver, RelativeSource={RelativeSource Mode=TemplatedParent}}" Value="True">
                      <Setter Property="Opacity" Value="0.7" />
                  </DataTrigger>
              </Style.Triggers>
          </Style>
      

      private void Image_MouseDown(object sender, MouseButtonEventArgs e)
      {
      var img = (Image)sender;
      var toggle = (ToggleButton)img.TemplatedParent;
      toggle.IsChecked = true;
      }

      BUT the triggers doesn't work. I mouseover and opacity doesn't change. I click opacity doesn't change :confused: Oh WPF makes me head not working. I is confusing.

      Greetings - Jacek

      1 Reply Last reply
      0
      • L Lutoslaw

        Hello again! It is another time I encounter this error: "objects added to an IDictionary must have a Key attribute or some other type of key associated with them.". I searched google to find out what is going on, but in every case the reason was that somebdy didn't set either Key or TargetType. I my case, I set the TargetType, but it doesn't work. In my previous post the problem was that I was using a UserControl. Now I try to set a template of a ToggleButton which is not a UserControl. WTH is going on? :confused:

        (...)

        The error started to appear since I changed something. I had tried to revert my recent changes but I couln't work out what is wrong :doh:
        EDIT: I've tried to set x:Key and reference Template={StRes key} and got exception:

        {"'Set property 'System.Windows.DataTrigger.Binding' threw an exception.' Line number '59' and line position '18'."}

        Inner:

        {"Unable to cast object of type 'System.Windows.TemplateBindingExpression' to type 'System.Windows.Data.BindingBase'."}

        Inner:null
        Edit2: IsChecked IS a dep property
        Any ideas?

        Greetings - Jacek

        modified on Monday, May 16, 2011 5:46 AM

        V Offline
        V Offline
        Venkatesh Mookkan
        wrote on last edited by
        #3

        When you write ControlTemplate you can use normal Triggers and EventTriggers. DataTriggers are meant to use along with DataTemplate. And also Key is a must for <ControlTemplete> whereas Style doesn't require one.

        Venkatesh Mookkan (My Recent Article: WPF Custom Control - FilterControl for ListBox/ListView)

        M L 2 Replies Last reply
        0
        • V Venkatesh Mookkan

          When you write ControlTemplate you can use normal Triggers and EventTriggers. DataTriggers are meant to use along with DataTemplate. And also Key is a must for <ControlTemplete> whereas Style doesn't require one.

          Venkatesh Mookkan (My Recent Article: WPF Custom Control - FilterControl for ListBox/ListView)

          M Offline
          M Offline
          markovl
          wrote on last edited by
          #4

          Yes, that's true. This worked for me:

          <ControlTemplate.Triggers>
          <Trigger Property="IsChecked" Value="True">
          <Setter Property="Opacity" Value="1" />
          </Trigger>
          </ControlTemplate.Triggers>

          You may need to supply TargetName for the Setter.

          V 1 Reply Last reply
          0
          • M markovl

            Yes, that's true. This worked for me:

            <ControlTemplate.Triggers>
            <Trigger Property="IsChecked" Value="True">
            <Setter Property="Opacity" Value="1" />
            </Trigger>
            </ControlTemplate.Triggers>

            You may need to supply TargetName for the Setter.

            V Offline
            V Offline
            Venkatesh Mookkan
            wrote on last edited by
            #5

            TargetName is not required in his case. :)

            Venkatesh Mookkan (My Recent Article: WPF Custom Control - FilterControl for ListBox/ListView)

            1 Reply Last reply
            0
            • V Venkatesh Mookkan

              When you write ControlTemplate you can use normal Triggers and EventTriggers. DataTriggers are meant to use along with DataTemplate. And also Key is a must for <ControlTemplete> whereas Style doesn't require one.

              Venkatesh Mookkan (My Recent Article: WPF Custom Control - FilterControl for ListBox/ListView)

              L Offline
              L Offline
              Lutoslaw
              wrote on last edited by
              #6

              Whoa! Thanks. :thumbsup:

              Greetings - Jacek

              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