0

I am new to WPF.I have usercontrol which has expander and datagrid

in my UserControlViewModel

    public ObservableCollection<MyResult> _results;
    public UserControlViewModel
    {
         _results = new ObservableCollection<MyResult>();
        _results.Add(new EntitySearchResults()
        {
            HeaderName= "Header1", //Expander header
            ColName= new ObservableCollection<string>() { "Col1"}
            CellValues = new ObservableCollection<string>() { "Val1" }       
        } 
    }

in my usercontrol.xaml

     <DataGrid
                 x:Name="dataGrid"....
                 ItemsSource="{Binding Path=CellValues }"
                 SelectedValue="{Binding Path=SelectedValue,
                                RelativeSource={RelativeSource                                                                                   
                                FindAncestor,AncestorType=local:MyUserControl,
                                AncestorLevel=1}}" // This is Dependency Property which gets me the Datagrid Row

      </DataGrid>

and In my Mainwindow I have

       <DataTemplate>
                <Grid>
                    <local:MyUserControl x:Name="mySearchControl" 
                    SelectedValue="{Binding 
                  DataContext.CurrentItem,ElementName=MyWindow,Mode=TwoWay}"/>
                </Grid>
     </DataTemplate>

and in my Mainwindow View Model

   public object CurrentItem
    {
        get { return currentItem; }
        set
        {
            currentItem = value;
            SetProperty(ref currentItem, value);
        }
    }

But this just gives me the Row values.I also need the HeaderName and ColumnName in Mainwindow.

How can I do it ?

Rohit
  • 10,056
  • 7
  • 50
  • 82
  • Firstly look at the [grouping in WPF](https://msdn.microsoft.com/en-us/library/ff407126%28v=vs.110%29.aspx) then you could use [MultiBinding](http://tech.pro/tutorial/809/wpf-tutorial-using-multibindings). HTH – XAMlMAX Feb 27 '15 at 08:11
  • its slightly different from my requirement – Rohit Feb 27 '15 at 10:52
  • 1
    Created a ViewModel for your UserControl? Yeah, that's wrong. Your UC should simply display whatever is bound to it. Or, in your case, to properties of your main window's VM. So it would be easy to get that info. But instead you're binding to a different view model, because you haven't had the experience yet where this screws you over. Welcome to screwing yourself up because you created a view model for your user control. –  Feb 27 '15 at 18:45
  • @Will http://stackoverflow.com/questions/1939345/wpf-should-a-user-control-have-its-own-viewmodel – Rohit Feb 28 '15 at 05:27
  • 2
    @kyle, while there are some answers from high-rating SO users meaning "yes, you can create viewmodels for controls", I suggest you to ask yourself: why there are no viewmodels for WPF `DataGrid` or `TreeView`? These controls are pretty complex too, so they can be treated as user controls. Additionally, I would suggest you to read carefully about the `DataContext` property of the `FrameworkElement`. – dymanoid Feb 28 '15 at 11:51
  • @kyle, to clarify my comment: of course, there's nothing wrong to create a viewmodel for a control while **using** it. But doing so to define the control's **logic** is a kind of code smell. – dymanoid Feb 28 '15 at 12:01
  • Those answers are terrible. The selected one is wishy washy without details. I'll add an answer there when I get time. –  Mar 02 '15 at 15:09
  • kyle next time you answer to my comment please [tag me in](http://stackoverflow.com/editing-help#comment-formatting), otherwise I will not get notified. Now your `UserControl` should not be separated if reused throughout your app as this introduces layer of complexity, and to be fair how did you design such user control, is it to conform with MvvM? @Will I can't see any answers? – XAMlMAX Mar 03 '15 at 09:10

0 Answers0