Fill in one combo box based on another

I am learning wpf mvvm and struggling with what, in my opinion, is simple but could not solve on my own.

What I want is the ability to select an item in a populated combo box, and then populate another combo box based on this selection. I can't seem to get the second combobox loaded in response to the selection.

I included an example with two combo boxes and one text block. When I launch the application and select an item in the first combo, the text block is updated based on the binding, but I do not know where to call the code to update the second combo box.

I tried adding a call to the set SelectedString element, but this seems to never be called. I am sure that I am missing something simple, but I need someone to help lift the veil!

I have tried suggestions for other posts, but so far I have not been successful.

Here is the haml for presentation:

<Window x:Class="ThrowAwayMVVMApp.Views.MainView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Main Window" Height="400" Width="800">

    <DockPanel>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="60*" />
                <RowDefinition Height="282*" />
            </Grid.RowDefinitions>
            <!-- Add additional content here -->
            <ComboBox ItemsSource="{Binding MyStrings}" SelectedItem="{Binding Path=SelectedString, Mode=TwoWay}" Height="23" HorizontalAlignment="Left" Margin="18,24,0,0" Name="comboBox1" VerticalAlignment="Top" Width="204" />
            <TextBlock Text="{Binding SelectedString}" Height="23" HorizontalAlignment="Left" Margin="276,24,0,0" Name="textBlock1" VerticalAlignment="Top" Width="227" Background="#FFFAE7E7" />
            <ComboBox ItemsSource="{Binding ResultStrings}"  Height="23" HorizontalAlignment="Left" Margin="543,25,0,0" Name="comboBox2" VerticalAlignment="Top" Width="189" />
        </Grid>
    </DockPanel>
</Window>

Here is a view model:

public class MainViewModel : ViewModelBase
{
    public MainViewModel()
    {
        this.MyStrings = new ObservableCollection<string>
            {
                "One",
                "Two",
                "Three",
                "Four",
                "Five"
            };
    }

    public ObservableCollection<string> MyStrings { get; set; }
    public ObservableCollection<string> ResultStrings { get; set; }

    public string SelectedString
    {
        get { return (string)GetValue(SelectedStringProperty); }
        set 
        { 
            SetValue(SelectedStringProperty, value);
            this.ResultStrings = getResultStrings(value);
        }
    }

    // Using a DependencyProperty as the backing store for SelectedString.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty SelectedStringProperty =
        DependencyProperty.Register("SelectedString", typeof(string), typeof(MainViewModel), new UIPropertyMetadata(""));


    private ObservableCollection<string> getResultStrings(string input)
    {
        ObservableCollection<string> result = null;

        if (input == "Three")
        {
            result = new ObservableCollection<string> { "Six", "Seven", "Eight" };
        }
        else
        {
            result = new ObservableCollection<string> { "Nine", "Ten", "Eleven" };
        }

        return result;
    }
}
+3
source share
2 answers

SelectedString: PropertyChangedCallback, , DP , CLR (. http://msdn.microsoft.com/en-us/library/system.windows.propertychangedcallback.aspx); , DP .

(, SelectedString) WPF CLR, getResultStrings.

, POCO View Models, INotifyPropertyChanged: DP - , ( System.Windows).

: http://kentb.blogspot.com/2009/03/view-models-pocos-versus.html

+2

Try

private string selectedString;
public string SelectedString
{
    get { return selectedString; }
    set 
    { 
        if (selectedString == value) return;
        selectedString = value;
        // Required for the UI to know the change was successful
        RaisePropertyChanged("SelectedString");
        LoadStringResults(value);
    }
}

private ObservableCollection<string> resultStrings;
public ObservableCollection<string> ResultStrings
{
    get { return resultStrings; }
    set 
    { 
        if (resultStrings== value) return;
        resultStrings= value;
        // Required for databinding to know that ResultStrings changed
        // Previously you changed this property without updating the UI
        RaisePropertyChanged("ResultStrings");
    }
}

private void LoadStringResults(string input)
{
    ObservableCollection<string> result = null;

    if (input == "Three")
    {
        result = new ObservableCollection<string> { "Six", "Seven", "Eight" };
    }
    else
    {
        result = new ObservableCollection<string> { "Nine", "Ten", "Eleven" };
    }

    ResultStrings = result;
}
0

Source: https://habr.com/ru/post/1759356/


All Articles