开发者

Simple Master/Detail scenario with combobox/datagrid in Silverlight 4

开发者 https://www.devze.com 2023-02-12 13:08 出处:网络
I\'m building a series of admin forms as part of my application. Many of the forms have this same scenario, and I\'m wondering what the bes开发者_如何学Got way to manage this is...

I'm building a series of admin forms as part of my application.

Many of the forms have this same scenario, and I'm wondering what the bes开发者_如何学Got way to manage this is...

Here's a screenshot of an example from the old winforms app. Not all of the forms are this simple, but I figure it's a good place to start.

The first field is always a combo box. After a user selects an item, the other fields populate with the associated data.

Currently, my ViewModel contains an ObservableCollection<circuit> (for this example). I'm not sure how to set up my bindings, given the master/detail scenario where your master (primary combobox) is also part of your detail (CircuitName field). I was thinking of pulling the combobox out and, after selecting an item, displaying a datagrid. Is there a better way? If I do go this route, I have read a few articles that indicate binding a combobox INSIDE the datagrid, while property binding the selectedValue prop is a total PITA. Obviously having a combobox would be required for a user to create/edit an existing CircuitName...

Your thoughts?


I'm not sure I fully understand, but if I was using MVVM Light, like I think you are:) And I wanted to replicated the screen above I would do something like this. But I'm probably not 100% understanding what you mean.

<Grid x:Name="LayoutRoot">
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>

    <StackPanel Grid.ColumnSpan="2">
        <TextBlock Text="Circuit Name"/>
        <ComboBox SelectedValue="{Binding SelectedCircuit,Mode=TwoWay}" ItemsSource="{Binding Circuits}" DisplayMemberPath="Name"/>

    </StackPanel>

    <StackPanel Grid.Column="0" Grid.Row="1">
        <TextBlock Text="Circuit Code"/>
        <TextBlock Text="{Binding SelectedCircuit.Code}"/>
    </StackPanel>

    <StackPanel Grid.Column="1" Grid.Row="1">
        <TextBlock Text="Voltage"/>
        <TextBlock Text="{Binding SelectedCircuit.Voltage}"/>
    </StackPanel>

    <Button Grid.Row="2" Content="Okay"></Button>
    <Button Grid.Row="2" Grid.Column="1" Content="Cancel"></Button>

</Grid>

using System.Collections.ObjectModel; using GalaSoft.MvvmLight; using MvvmLight1.Model;

namespace MvvmLight1.ViewModel {

public class MainViewModel : ViewModelBase
{

    public MainViewModel()
    {

        Circuits = new ObservableCollection<Circuit>
                       {
                           new Circuit {Code = "123123", Name = "Test1", Voltage = 2.2, Id = 1},
                           new Circuit {Code = "14224", Name = "Test2", Voltage = 3.2, Id = 2},
                           new Circuit {Code = "54234", Name = "Test3", Voltage = 4.2, Id = 3},
                       };

    }

    public ObservableCollection<Circuit> Circuits { get; set; }

    private Circuit _selectedCircuit;
    public Circuit SelectedCircuit
    {
        get { return _selectedCircuit; }
        set { _selectedCircuit = value;
        RaisePropertyChanged("SelectedCircuit");
        }
    }
}

}

namespace MvvmLight1.Model {

public class Circuit
{
    public int Id { get; set; }

    public string Name { get; set; }

    public string Code { get; set; }

    public double Voltage { get; set; }

}

}


Answer was really simple actually.

I have my ObservableCollection<Circuit> Circuits that's loaded in the VM Constructor. I created a ObservableCollection<Circuit> SelectedCircuit. My xaml then looks like this:

<ComboBox ItemsSource="{Binding Configs}"
          SelectedValue="{Binding SelectedConfig, Mode=TwoWay}"
          DisplayMemberPath="SwitchCode"
                 />
<toolkit:DataForm CurrentItem="{Binding SelectedConfig}" AutoEdit="False"  />
0

精彩评论

暂无评论...
验证码 换一张
取 消