0

I'm trying to automatically generate a button list from a XML file.

Here is my XML:

<WPFDS>
    <Title>Hospital</Title>
    <BUTTONS>
        <BUTTON id="1" visible="1">
              <Text>Content1</Text>
              <Program>b1_Click<Program>
        </BUTTON>
        <!-- ... -->
    </BUTTONS>
</WPFDS>

The code to make WPF automatically generate the "button list":

<XmlDataProvider x:Key="wpfds"            
                   Source="Config/Config.xml"           
                   XPath="/WPFDS"           
                   IsAsynchronous="False"           
                   IsInitialLoadEnabled="True"  
/>
<!-- ... -->
<ListBox x:Name="lbBotones" 
         ItemsSource="{Binding Source={StaticResource wpfds}, XPath=./BUTTONS/BUTTON}" 
         Margin="0,19,0,354" Grid.Row="1" IsEnabled="True" Width="607"                     
         HorizontalContentAlignment="Stretch" BorderBrush="{x:Null}" 
         Background="{x:Null}" 
         ScrollViewer.HorizontalScrollBarVisibility="Disabled"
         ScrollViewer.VerticalScrollBarVisibility="Disabled" Height="346"
>
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel IsItemsHost="True" Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <ListBox.ItemTemplate>                    
        <DataTemplate>                        
            <Button Height="50" Width="150" Margin="70,10"
                    Content="{Binding XPath=./Text}"                                    
            />                        
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

With this, the list of buttons is generated correctly, but the problem is that I also need to "automatically" assign a Click Event to each Button. Is it possible to specify the click event from the XML document? Something like:

Click="{Binding XPath=./Program}"

But it doesn't work.

Thanks for your help!

Zoot
  • 2,217
  • 4
  • 29
  • 47
MorgoZ
  • 2,012
  • 5
  • 27
  • 54
  • possible duplicate of [Binding Button click to a method](http://stackoverflow.com/questions/3531772/binding-button-click-to-a-method) – Tomalak Dec 13 '11 at 13:48
  • +1 Duplicate. See other question for a solution (command binding). – jv42 Dec 13 '11 at 14:07

1 Answers1

0

This cannot work, because event handlers are resolved at compile time; in your case the name of the event handler wouldn't be known until runtime. And anyway, you can't set a binding on an event (although it will be possible in .NET 4.5).

Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758