tags:

views:

174

answers:

2

Ok so.. I'm a bit lost. What i have is a

window1.xaml

just a combobox that selects different Enviroments

then i have a which displays data based on environment

I have a default environment when app loads up and displays the correct data.

Question: I want the Dashboards data to change when a different Environment is selected.

I've been struggling with this for a day now and I'm not getting anywhere.... Any help would be appreciated...

Code so far:

EnvironmentView.xaml

 <UserControl.DataContext>
        <vm:EnvironmentViewModel />
    </UserControl.DataContext>
    <StackPanel Orientation="Horizontal">
        <TextBlock HorizontalAlignment="Stretch" VerticalAlignment="Center" Text="Environment:" Margin="0,0,5,0" />
        <ComboBox  x:Name="environments"  ItemsSource="{Binding Data}" SelectedItem="{Binding Data}" SelectedIndex="0" ></ComboBox>
    </StackPanel>

EnivronmentViewModel.cs

  public class EnvironmentViewModel : BaseModelView {
        public ObservableCollection<string> _data;
        public ObservableCollection<string> Data
        {
            get {
                if (this._data == null)
                {
                    _data = new ObservableCollection<string>();
                    foreach (ConnectionStringSettings connectionString in ConfigurationManager.ConnectionStrings)
                    {
                        this._data.Add(connectionString.Name);
                    }
                }
                return this._data;
            }
            set {
                if (_data == value) return;
                _data = value;
                OnPropertyChanged("Data");
            }
        }

DashboardView.xaml

<UserControl.DataContext>
        <viewmodel:DashboardViewModel  />
    </UserControl.DataContext>
    <Border Style="{StaticResource ThickBorderStyle}">
        <StackPanel>
            <TextBlock Text="Current Bugs" />
            <data:DataGrid x:Name="dataview" Width="Auto" Height="Auto" ItemsSource="{Binding Data}">
            </data:DataGrid>
        </StackPanel>
    </Border>

DashboardViewModel.cs

private DashboardRepository _repository;
        public ObservableCollection<DashBoardCount> _data;
        public DashboardViewModel() {
            _repository = new DashboardRepository();
        }

        public ObservableCollection<DashBoardCount> Data
        {
            get
            {
                if(this._data==null)
                {
                    IQuery q = new Query() { ConnectionStringKey = this.ConnectionStringKey };
                    _data = this._repository.Get(q);
                }
                return _data;
            }
            set {
                if (_data == value) return;
                _data = value;
                OnPropertyChanged("Data");
            }
        }
A: 

You show bind the SelectedItem on the ComboBox to another property in the ViewModel (not the collection).

Then add a button and bind the Command to a ICommand in your ViewModel. This Command should use the selectedItem to show that dashboard.

(Maybe is there a way to do it without adding a button, but I'm a MVVM newbie myself and don't know how)

Eduardo Molteni
A: 

There are quite a few ways to implement this...

My suggestion would be that right now you have two separate "views/models" - and you would need something to combine / integrate them - which you could do using the MVVM pattern again - I am assuming that right now window1.xaml is meant to do this.

So you could rename the window1.xaml to something along the lines of mainView.xaml, then add a mainViewModel.cs. Then, like Eduardo said, make an accessor but put it in your mainViewModel.cs - you could call it something like selectedEnvironment & selectedDashboard. If you then had your usercontrols "Enviroment" & "Dashboard" on the mainView you could bind them.

Right now though, looking at your ViewModels, I am not sure how you are intending on passing the relevant data based on the environmentModel to the dashboardModel - you would need to set some sort of relationship up - you could either do this within the EnviromentViewModel or the mainViewModel... but I cannot see any explicit relationship right now? If you are intending on the relationship being based on the accessor Data, you need to make this of a common type?

Also, on a side note I am note sure why you made _data as public, since you have created public accessors Data, surely you would want to keep _data contained?

Mark Pearl