Fix hjälp med en lösning.
code-behind
public partial class Window1 : Window
{
public static readonly DependencyProperty CompanyPositionProperty = DependencyProperty.Register("CompanyPosition", typeof(ObservableCollection<CompanyPosition>), typeof(Window1));
public ObservableCollection<CompanyPosition> CompanyPosition {
get { return (ObservableCollection<CompanyPosition>)GetValue(Window1.CompanyPositionProperty); }
set { SetValue(Window1.CompanyPositionProperty, value); }
}
public Window1() {}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.CompanyPosition = new ObservableCollection<CompanyPosition>();
this.CompanyPosition.Add(new CompanyPosition(1, "Big boss"));
this.CompanyPosition.Add(new CompanyPosition(2, "Small boss"));
this.CompanyPosition.Add(new CompanyPosition(3, "Worker"));
DataContext = new Person();
}
}
public class Person
{
public string Name { get; set; }
public int SelectedActivator { get; set; }
public Person()
{
this.Name = "Magnus";
this.SelectedActivator = 2;
}
}
public class CompanyPosition
{
public int Key { get; set; }
public string Value { get; set; }
public CompanyPosition(int key, string value)
{
this.Key = key;
this.Value = value;
}
}
XAML-filen
<Window x:Class="WpfApplication1.Window1" x:Name="_root"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300" Loaded="Window_Loaded">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBox Grid.Row="0" Name="txtName" Text="{Binding Path=Name}" Height="20"/>
<ComboBox Grid.Row="1" Name="cbActivator" ItemsSource="{Binding
Path=CompanyPosition, ElementName=_root}" SelectedValue="{Binding Path=SelectedActivator}"
SelectedValuePath="Key" DisplayMemberPath="Value" Height="20"/>
</Grid>
</Window>