objects added to an IDictionary must have a Key attribute [modified]
-
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
-
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
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
-
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
When you write
ControlTemplate
you can use normalTriggers
andEventTriggers
.DataTriggers
are meant to use along withDataTemplate
. 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)
-
When you write
ControlTemplate
you can use normalTriggers
andEventTriggers
.DataTriggers
are meant to use along withDataTemplate
. 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)
-
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.TargetName is not required in his case. :)
Venkatesh Mookkan (My Recent Article: WPF Custom Control - FilterControl for ListBox/ListView)
-
When you write
ControlTemplate
you can use normalTriggers
andEventTriggers
.DataTriggers
are meant to use along withDataTemplate
. 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)