4
votes

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?

2
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

2 Answers

2
votes

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.

1
votes

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.