4

I want to bind my MultiDataTrigger.Conditions to radio button, but it's not working. here is my scenario.

In wpf, I have a GridPanel, which suppose to have radiobuttons in it.

I generate dynamic radiobuttons into GridPanel, like this:

RadioButton allAccountBtn = new RadioButton();
allAccountBtn.Name = "allAccountBtn";
GridPanel.Children.Add(allAccountBtn);

then in my xaml, I had an image button which will change based in this radiobutton selection, and another control's property.

here is my code:

<Button>
 <Button.Template>
        <ControlTemplate>
               <Image Name="addFolderIcon" Source="Icon/Decoration/folderColor.png">
               <ControlTemplate.Triggers>
                       <MultiDataTrigger>
                             <MultiDataTrigger.Conditions>
                                    <Condition Binding="{Binding Tag, ElementName=folderBackBtn}" Value="{x:Null}"/>
                                    <Condition Binding="{Binding IsChecked, ElementName=allAccountsBtn}" Value="True"/>
                             </MultiDataTrigger.Conditions>
                             <Setter Property="Control.IsEnabled" Value="False"/>
                             <Setter TargetName="addFolderIcon" Property="Source" Value="Icon/Decoration/folder.png"/>
                       </MultiDataTrigger>
               </ControlTemplate.Triggers>
         </ControlTemplate>
 </Button.Template>
</Button>

When I run the program, it display error code:"Cannot find source for binding with reference", and the conditions return false for second condition(elementName=allAccountsBtn)

Why is this happen?

Any way can I refer to get IsChecked property from dynamically named and generated RadioButton?

VHanded
  • 2,079
  • 4
  • 30
  • 55
  • If I add radio button in my xaml, it can be detected, so the problem now is, ElementName can't find dynamically generated control. – VHanded Dec 14 '10 at 02:32

2 Answers2

2

I noticed you're setting Name property on your dynamically generated object to "allAccountBtn" (singular "Account"), but your XAML's ElementName is referencing an "allAccountsBtn" (plural "Accounts"). Try changing them to be the same.

yagni
  • 1,160
  • 13
  • 15
1

Let's localize the issue. Try to add RadioButton directly in XAML to your GridPanel, not in C#. If error persists, then the issue is not with the dynamic way of adding a control.

I think there's an issue with the 'visibility' of controls under templates. You can call yourButton.ApplyTemplate() to force building of the visual tree. But not sure how that is done in the XAML.

TarasB
  • 2,407
  • 1
  • 24
  • 32
  • Yes, the problem is the dynamic generated RadioButton. I add RadioButton locally, and it run's no error. I call GridPanel.ApplyTemplate() after I call GridPanelChildren.Add(allAccountsBtn), nothing happen. Same happen to allAccountsBtn.ApplyTemplate()... I must include the RadioButton dynamically, because the data is from database. – VHanded Dec 13 '10 at 10:17
  • Read from somewhere, the dynamically generated control has different namespace with local control, but I got no idea how to deal with this clue.. – VHanded Dec 13 '10 at 10:21