MVVMを使用するプロジェクトで作業しており、ユーザーがデータベースサーバーに接続し、使用するサーバー上のデータベース領域の一覧が表示されます。領域のリストについては、ObservableCollectionにバインドされたItemSourceを持つComboBoxを使用しています。 ViewModelには WPF ObservableCollectionがビュー内で更新されない
ComboBox ItemsSource="{Binding AreaCollecton}" SelectedItem="{Binding SelectedArea}"
は、私はこのように定義されたのObservableCollectionを持っている:
private ObservableCollection<string> areaCollection = new ObservableCollection<string>();
public ObservableCollection<string> AreaCollection
{
get { return areaCollection; }
set{
areaCollection = value;
OnPropertyChanged("AreaCollection");
}
}
サーバー名を入力したとき、コレクションが更新されると、[接続]ボタンをクリックします。 Clickイベントは、使用可能な領域について入力されたサーバーをポーリングし、リストをViewModelに返します。
public void Connect()
{
areaCollection.Clear();
serverConnection = new ServerConnect();
areaList = serverConnection.getAreaList(server);
//areaList is defined as a Dictionary<string, string>
foreach(KeyValuePair<string, string> area in areaList)
{
areaCollection.Add(area.Key);
}
OnPropertyChanged("AreaCollection");
}
私は(ボタンのクリックイベントから呼び出された)Connectメソッドでのforeachにブレークポイントを入れて、私はを通じて段階として、私は予想通りareaCollectionがareaListから更新されました。ただし、areaCollectionの作成が完了すると、ViewのComboBoxには何も表示されません。
私はWPFとMVVMにはかなり新しいので、私はここに何か小さなものがあると感じています。私は似たような問題に関する他の記事を見てきましたが、そこに提案を実装しましたが、まだビューを更新していないので、私は何が欠けているのか分かりません。どんな助けでも大歓迎です。
EDIT: ComboBoxのラインよりも役立つ場合は、完全なXAMLを追加します。
<Grid Height="400" Width="400" VerticalAlignment="Center" HorizontalAlignment="Center">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="1.5*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="50" />
<RowDefinition Height="30" />
<RowDefinition Height="30" />
<RowDefinition Height="30" />
<RowDefinition Height="30" />
<RowDefinition Height="30" />
<RowDefinition Height="30" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Grid.ColumnSpan="2" HorizontalAlignment="Center" VerticalAlignment="Center" x:Name="lblPrompt" TextWrapping="Wrap" Text="Enter Laboratory login and connection info:" FontSize="18" />
<Label Style="{StaticResource ConnectLabel}" Grid.Row="1" Grid.Column="0" Content="Server:" />
<TextBox Style="{StaticResource ConnectTextBox}" Grid.Row="1" Grid.Column="1" x:Name="txtbxServerName" TextWrapping="Wrap" TabIndex="3" Text="{Binding Server}" />
<Button Style="{StaticResource ConnectButton}" Grid.Row="2" Grid.Column="1" x:Name="btnConnect" Content="Connect" FontSize="14">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<ei:CallMethodAction TargetObject="{Binding}" MethodName="Connect" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
<Label Style="{StaticResource ConnectLabel}" Grid.Row="3" Grid.Column="0" Content="Area:" />
<ComboBox Style="{StaticResource ConnectCombo}" Grid.Row="3" Grid.Column="1" x:Name="txtbxAreaName" TabIndex="4" ItemsSource="{Binding AreaCollecton}" SelectedItem="{Binding SelectedArea}" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<ei:CallMethodAction TargetObject="{Binding}" MethodName="AreaConnect" />
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
<Label Style="{StaticResource ConnectLabel}" Grid.Row="4" Grid.Column="0" Content="Lab Username:" />
<TextBox Style="{StaticResource ConnectTextBox}" Grid.Row="4" Grid.Column="1" x:Name="txtbxUsername" TextWrapping="Wrap" TabIndex="1" Text="{Binding Username}" />
<Label Style="{StaticResource ConnectLabel}" Grid.Row="5" Grid.Column="0" Content="Lab Password:" />
<PasswordBox Margin="2" Grid.Row="5" Grid.Column="1" x:Name="pwdbxPassword" TabIndex="5" PasswordChanged="PasswordBox_PasswordChanged" />
<Button Style="{StaticResource ConnectButton}" Grid.Row="6" Grid.Column="1" x:Name="btnLogin" Content="Login" FontSize="14">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<ei:CallMethodAction TargetObject="{Binding}" MethodName="Login" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
<TextBlock Grid.Row="7" Grid.ColumnSpan="2" HorizontalAlignment="Center" Margin="10" TextWrapping="Wrap" FontSize="14" Text="{Binding Status}" />
</Grid>
@Amit Kumarが指摘しているように、タイプミスのようです – taquion
私はそれを逃したと信じられません。この間、私の脳を壊してしまった。 –
未来のために。デバッグ時に、出力ウィンドウに注目し、DataBinding例外を探します。この場合、そのプロパティの例外が見つかりませんでした。 – taquion