XAML binding on click
Continuing from my last question, I'd like to know how can I bind when a button is clicked (can this be done through pure XAML?) - or more simply, how can I do XAML-like binding through C# code?
EDIT: The previous question containing info:
I want to create a listbox that'll be bound to XPath, relative to the other listbox's currently selected item.
It's using XmlDataProvider for the data, and the XML file looks like this:
<Programs>
<Program name="...">
<Step name="..."/>
<Step name="..."/>
</Program>
<Program name="another">
...
</Programs>
So, the "parent" listbox is listing out all the programs, while "child" shows only Steps from the current Program. I just need a pointer on the fact what's such type of 开发者_如何学编程binding called.
Thanks in advance.
End of previous question
The question is how can I do a one time bind (I do not want binding to change as soon as user clicks another listbox item) when a button called "Load" is pressed?
Let's start with an example where the detail list box does change whenever the user selects an item in the master list box:
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Page.Resources>
<XmlDataProvider x:Key="Programs">
<x:XData>
<Programs xmlns="">
<Program name="Some program">
<Step name="Some program step 1"/>
<Step name="Some program step 2"/>
<Step name="Some program step 3"/>
<Step name="Some program step 4"/>
</Program>
<Program name="Some other program">
<Step name="Some other program step 1"/>
<Step name="Some other program step 2"/>
<Step name="Some other program step 3"/>
<Step name="Some other program step 4"/>
</Program>
</Programs>
</x:XData>
</XmlDataProvider>
</Page.Resources>
<DockPanel>
<Label DockPanel.Dock="Top">Programs</Label>
<ListBox x:Name="Program" DockPanel.Dock="Top" ItemsSource="{Binding Source={StaticResource Programs}, XPath=/Programs/Program}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding XPath=@name}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Label DockPanel.Dock="Top">Steps</Label>
<ListBox x:Name="Step" DockPanel.Dock="Top" DataContext="{Binding ElementName=Program, Path=SelectedItem}" ItemsSource="{Binding XPath=Step/@name}"/>
</DockPanel>
</Page>
What has to change in order for the detail to change only when the user clicks a button? One thing: the DataContext
on the ListBox
named Step
. You need to use code-behind to do this, but the code is pretty simple:
private void Button_Click(object sender, RoutedEventArgs e)
{
if (Program.SelectedItem != null)
{
Step.DataContext = Program.SelectedItem;
}
}
Edit
In order for the above to work, you have to change the Step
ListBox
to:
<ListBox x:Name="Step" DockPanel.Dock="Top" ItemsSource="{Binding XPath=Step/@name}"/>
That ListBox
won't display anything unless its DataContext
is assigned.
Bind the Command property to a property implementing ICommand.
Assuming you have an object (ViewModel) with property "ICommand HelloCommand { get { ... }; }" set as the data context:
<Button Content="Hello" Command="{Binding HelloCommand}"/>
The Execute method of the ICommand implementation will get called when the button is clicked. The CanExecute method should return whether the command is available and will determine the enabled status of the button.
There are also helper classes and frameworks that can make this simpler (and/or more complicated).
精彩评论