2016-06-13 22 views
-1

私のアプリケーションにはコンボボックスコントロールがあります。私はそれを使用して、データベースで定義された状態のリストから状態を選択します。選択された値は、ユーザークラスの状態フィールド(a)に値を割り当てるために使用する必要があります(データベースにユーザーデータを保持するテーブルがあります)。複数のDataContextへのコンボボックスのバインド

XAML:背後に

<StackPanel x:Name="stckDetails"> 
    <TextBox Margin="10,5,10,5" Text="{Binding FirstName, Mode=TwoWay}"/> 
    <TextBox Margin="10,5,10,5" Text="{Binding LastName, Mode=TwoWay}"/> 
    <ComboBox Margin="10,5,10,5" ItemsSource = "{Binding Register.States}" Text="{Binding State, Mode=TwoWay}"/> 
</StackPanel> 

コード:

class Register 
{ 
    public User newUser; 
    public ObservableCollection<string> States { get; set; } 
    public Register() 
    { 
    newUser = new User(); 
    States = new ObservableCollection<string> { "CH", "MA", "KL", "FL" }; 
    stckDetails.DataContext = newUser; 
    } 
} 


public class User 
{ 
    public int UserId { get; set; } 

    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public string State {get; set; } 
} 

私は別のDataContextにコンボボックスの異なる特性をバインドするにはどうすればよいです。出来ますか ?構文私はdoesntの仕事を使用しています。

答えて

0

私はいくつかの変更を加えました。下記のコードを参照してください。本当に便利でした

<Window x:Class="DataContext_Learning.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:local="clr-namespace:DataContext_Learning" 
    mc:Ignorable="d" 
    Title="MainWindow" Height="350" Width="525"> 
<Grid> 
    <StackPanel x:Name="stckDetails"> 
     <TextBox Margin="10,5,10,5" Text="{Binding NewUser.FirstName, Mode=TwoWay}"/> 
     <TextBox Margin="10,5,10,5" Text="{Binding NewUser.LastName, Mode=TwoWay}"/> 
     <ComboBox Margin="10,5,10,5" ItemsSource = "{Binding States}" Text="{Binding State, Mode=TwoWay}"/> 
    </StackPanel> 
</Grid> 

namespace DataContext_Learning 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
      this.DataContext = new Register(); 
     } 
    } 

    class Register 
    { 
     public User NewUser { get; set; } 
     public ObservableCollection<string> States { get; set; } 
     public Register() 
     { 
      NewUser = new User(); 
      States = new ObservableCollection<string> { "CH", "MA", "KL", "FL" }; 
     } 
    } 


    public class User 
    { 
     public int UserId { get; set; } 

     public string FirstName { get; set; } 
     public string LastName { get; set; } 
     public string State { get; set; } 
    } 
} 
+0

。 – BlackMask

関連する問題