Learning MVVM\WPF\C# by building a simple master\details app. I have a MainView which holds the MasterView, DetailView, and ControlsView. I have added buttons (ICommand) to all views. They all work properly except the one on the DetailView. The problem is the view's datacontext (DataContext="{Binding SelectedMediaItem}"). On the DetailView, if I add a main grid to hold the SelectedMediaItem grid and then add a button the main grid it works fine. Any ideas how I can add a button next to a textbox in the SelectedMediaItem grid? Thank You
Asked
Active
Viewed 369 times
-2
-
Try to use paragraphs and add a little more code. And a better description, I got lost. – H H Jul 26 '11 at 16:40
-
Quite difficult to understand your issue, is it with databiding or command, can you post xaml and code-behind? – anivas Jul 26 '11 at 16:56
-
The problem is that setting the DetailView's datacontext to the collection's CurrentItem removes the ability to add buttons to that view. The buttons use commands, such as Command="{Binding SaveCommand}" and they work great except when under the the view's current item datacontext. – Lone Starr Jul 26 '11 at 17:20
-
And who publishes those commands? etc etc. Add a code outline. – H H Jul 26 '11 at 19:32
-
**Hopefully, this will make the problem more understandable. The MasterView's datacontext is "AA", the DetailView's datacontext is "CurrentItem of AA". I need to add a button to the DetailView with it's datacontext binding to just "AA" NOT "CurrentItem of AA" – Lone Starr Jul 26 '11 at 19:43
1 Answers
0
During my learning of MVVM\WPF\C# I sometime find it difficult to find the proper words to ask my questions. Nevertheless, I have managed to find an answer to my question.
<Grid DataContext="{Binding AA}">
<Grid DataContext="{Binding CurrentItem}">
<TextBox Text="{Binding Name}" />
</Grid>
<Button Command="{Binding SomeCommand}" />
</Grid>
This code puts the TextBox under the DataContext of "CurrentItem of AA" and the button under the DataContext of "AA". I then use rows,columns and margins to position the button next to the TextBox.
HTH

biju
- 17,554
- 10
- 59
- 95

Lone Starr
- 65
- 2
- 8
-
You can also replace the Grid tags with StackPanel or DockPanel to get an easier layout (without margins) – H H Jul 30 '11 at 09:54