0

Hi my Problem is I haven't any Idea how I send Data from my UserControls to other UserControls or Windows in my Project.

My Project is a .Net 7 WPF-Project.

I have one FilterControl where Checkboxes and/or Textboxes filtering a List. The List exists in this FilterControl.

And after this filtering I want to send die filtered List to an other UserControl to show the List.

Aside from that I want to send the List to an Window.

How can I do that?

This is the MainWindow

<Window x:Class="MyProject.MainWindow"
        xmlns:FilterCtrl="clr-namespace:MyProject.FilterControl"
        xmlns:ListCtrl="clr-namespace:MyProject.ListControl">
    <Window.DataContext>
        <local:MainViewModel/>
    </Window.DataContext>
    <Grid>
        <FilterCtrl:FilterControl/>
        <ListCtrl:ListControl/>
    </Grid>
</Window>

The UserControls:

SubFilter

<UserControl x:Class="MyProject.SubFilter">
    <UserControl.DataContext>
        <local:SubFilterViewModel/>
    </UserControl.DataContext>
    <Grid>
        <CheckBox Command="{Binding CheckBoxCommand1}" Content="FilterOption1" Checked="{Binding CheckBool1}">
        <CheckBox Command="{Binding CheckBoxCommand2}" Content="FilterOption2" Checked="{Binding CheckBool2}">
    </Grid>
</UserControl>

FilterControl

<UserControl x:Class="MyProject.FilterControl
             xmlns:SubFilterCtrl="clr-namespace:MyProject.SubFilterControl">
    <UserControl.DataContext>
        <local:FilterViewModel/>
    </UserControl.DataContext>
    <Grid>
        <SubFilterCtrl:SubFilterControl/>
        <TextBox Command="{Binding TextBoxCommand}" Text="AnotherFilterOption">
    </Grid>
</UserControl>

ListControl

<UserControl x:Class="MyProject.ListControl
             xmlns:ListCtrl="clr-namespace:MyProject.ListControl">
    <UserControl.DataContext>
        <local:FilterViewModel/>
    </UserControl.DataContext>
    <Grid>
        <ListView MaxHeight="500" ItemsSource="{Binding FilteredList}" SelectedItem="{Binding SelectedColumm}">
            <ListView.View>
                <GridView AllowsColumnReorder="true">

                    <GridViewColumn DisplayMemberBinding="{Binding Path=Name}" Header="Name" Width="200"/>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</UserControl>

I Use an Relay Command from this Side: RelayCommand

The List for Filtering are simple Objects with some Properties. Here only Name.

With the SubFilter I want only filtering with Checkboxes, and want back a List with the booleans from the CheckBoxes The FilterControl filtering with these List from SubFilter and his own FilterOptions. The ListControl should only show the List.

  • As any other controls, UserControls must not set their own DataContext and thus create their own, private view model which is disconnected from the view model structure of the application. They should instead inherit the DataContext from their parent element, e.g. a ContentControl where a DataTemplate is applied that creates the control instance. See [Data Templating Overview](https://learn.microsoft.com/en-us/dotnet/desktop/wpf/data/data-templating-overview?view=netframeworkdesktop-4.8). – Clemens Feb 17 '23 at 07:43
  • Should then all Variables in the ViewModels for the UserControls in the Parent Element, (MainWindow)? – FelixFeuerdorn Feb 17 '23 at 07:50
  • A view model can have properties that hold sub view models. There would typically be one sub view model per sub view. – Clemens Feb 17 '23 at 08:34
  • So if I have my MainViewModel, can I use FilterViewModel and ListViewModel if I declare these both ViewModels as Variables in my MainViewModel? Should I set the DataContext for the Controls in the MainWindow.xaml as MainViewModel or the specific VMs. I think this is the same as I did it, but in a another way... – FelixFeuerdorn Feb 17 '23 at 09:19
  • You would for example have a DataTemplate with `DataType="{x:Type local:FilterViewModel}"` that contains a FilterControl. Then you set `Content="{Binding FilterViewModel}"` on a ContentControl, where FilterViewModel is a property of the current DataContext, i.e. the main view model. The DataTemplate would automatically by used as ContentTemplate of the ContentControl and instantiate the FilterControl, which in turn automatically inherits its DataContext from the ContentControl. – Clemens Feb 17 '23 at 09:23

1 Answers1

-1

Now I have this in my MainWindow.xaml:

<Window x:Class="MyProject.MainWindow"
    xmlns:FilterCtrl="clr-namespace:MyProject.FilterControl"
    xmlns:ListCtrl="clr-namespace:MyProject.ListControl">
<Window.DataContext>
    <local:MainViewModel/>
</Window.DataContext>
    <Grid>
        <FilterCtrl:FilterControl DataContext:"{Binding Main}"/>
        <ListCtrl:ListControl DataContext:"{Binding Main}"/>
    </Grid>
</Window>

In the MainViewModel I have the Variable:

public MainViewModel Main {get { return this;}}

because I want to change the Datacontext if I have an other Window who uses there, but with other Design-Options

  • This makes no sense. You set the DataContext of the controls to a value that is already has. You may want to write `` where FilterViewModel is a property of MainViewModel. – Clemens Feb 17 '23 at 09:26
  • Okay, MainViewModel should have Variables for the (Sub-)ViewModels. And as you explained, can I set the DataContext from these Controls with your Example. Are then the Bindings for the Controls in they're 'ViewModels'? If I do this how communicate they with each other? Sorry I doesn't understand this Part with the DataTemplate and ContentControl. – FelixFeuerdorn Feb 17 '23 at 09:38
  • Not "variables", but public properties. Communication between the child view models could be done in many ways. They could know each other and call each others methods or use events, or the communication could be done via the main view model. – Clemens Feb 17 '23 at 09:44
  • How can my SubFilterViewModel call a FilterViewModel-Method? I would say I declare a FilterViewModel in MainViewModel and in the Constructor I declare the SubFilterViewModel and send 'this' (FilterViewModel) in the Constructor for the SubFilterViewModel. The SubFilter call then a FilterMethod with the FilterProperty he uses by any Action in the SubFilter.xaml, the Filter use the Information from Subfilter an filtering the List, then send the List to the ListViewModel in the Same way as SubFilter an Filter. Is this a solution I could use or is this bad? – FelixFeuerdorn Feb 17 '23 at 10:33
  • Give it a try... – Clemens Feb 17 '23 at 10:34