user1789782
user1789782

Reputation: 197

WPF ComboBox Custom Binding

I have a custom class with two values being description and url which is attached to a ComboBox. The ComboBox attaches to the class and populates as expected, but I cannot the the url value to fill the text attribute of the ComboBox.

            <ComboBox x:Name="platform_url" Grid.Column="1"  HorizontalAlignment="Left" Height="Auto" Margin="3" Grid.Row="0" VerticalAlignment="Center" Width="120"
                    Text="{Binding url, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}"
                    ItemsSource="{Binding LocationList}"
                    SelectedItem="{Binding SelectedLocation}"
                      DisplayMemberPath="Description"
                    SelectedValuePath="Url"
                      />

With the this uses the following class for data population and control:

class LocationViewModel : INotifyPropertyChanged
{
    public ObservableCollection<Location> LocationList { get; set; }
    public LocationViewModel()
    {
        LocationList = new ObservableCollection<Location>();
        LocationList.Add(new Location() { Description = "North America", Url = "abc.com" });
    }

    private Location selectedLocation;
    public Location SelectedLocation
    {
        get { return selectedLocation; }
        set
        {
            selectedLocation = value;
            OnPropertyChanged("SelectedLocation");
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
}
class Location
{
    public string Url { get; set; }
    public string Description { get; set; }
}

The best I can get is the description field being used, but not the url field.

Please help!

Upvotes: 2

Views: 102

Answers (1)

user1789782
user1789782

Reputation: 197

Got it working

                    <ComboBox x:Name="platform_datacenter" Grid.Column="1"  HorizontalAlignment="Left" Height="Auto" Margin="3" Grid.Row="0" VerticalAlignment="Center" Width="100"
                        SelectedValuePath="moid"
                        SelectedValue="{Binding Path=datacenter, Mode=TwoWay}" 
                        DisplayMemberPath="datacenter"/>

Upvotes: 1

Related Questions