私は WPF を試していて、以前に Silverlight でいくつかの作業を行い、DataContext、Data Binding、および INPC を正常に使用したことがありますが、理解できない問題に遭遇しました。今回は行き詰まり…
このコードを使用して、アプリケーションのメイン ウィンドウのインスタンスを作成しています。
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
MainWindow window = new MainWindow();
var viewModel = new MainVM(); //implements INotifyPropertyChanged
viewModel.Load("Brighton+UK");
window.DataContext = viewModel;
window.weatherList.ItemsSource = viewModel.WeatherInfo; //THIS WORKS
window.Show();
}
このようにアプリケーションを実行すると、すべて問題なく、メイン ウィンドウの ListBox に、MainVM の WeatherInfo ObservableCollection で見つかった項目が表示されます。
ただし、行をコメントアウトしてメイン ウィンドウの XAML に移動し、XAML で weatherList ListBox の ItemsSource プロパティを次のように設定すると、次のようになります。
<ListBox x:Name="weatherList"
Grid.Row="0"
ItemContainerStyle="{StaticResource stretched}"
ItemsSource="{Binding WeatherInfo}" />
MainWindow の DataContext を MainVM のインスタンスに設定していますが (C# コードの抜粋に示されているように)、期待どおりにリストにデータが取り込まれません。
誰かが私に説明してもらえますか?なぜですか?
==編集==
メイン ウィンドウのすべての XAML:
<Window x:Class="DataTemplates.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Google World Weather"
SizeToContent="WidthAndHeight"
WindowStartupLocation="CenterScreen"
xmlns:local="clr-namespace:DataTemplates" >
<!--Resources section-->
<Window.Resources>
<!--Styles-->
<Style TargetType="Label">
<Setter Property="FontSize" Value="24" />
<Setter Property="Margin" Value="10,0,0,0" />
</Style>
<Style x:Key="stretched" TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</Window.Resources>
<!--Command binding definition-->
<Window.CommandBindings>
<CommandBinding Command="Refresh" x:Name="cmdLoadWeatherForecast" CanExecute="cmdLoadWeatherForecast_CanExecute" Executed="cmdLoadWeatherForecast_Executed" />
</Window.CommandBindings>
<!--UI design - layout of individual controls-->
<Grid x:Name="LayoutRoot">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ListBox x:Name="weatherList" Grid.Row="0" ItemContainerStyle="{StaticResource stretched}" ItemsSource="{Binding WeatherInfo}" />
<StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Center" Margin="10" >
<TextBox Text="Brighton UK" Name="txtLocation" Width="200" FontSize="20" Margin="10" FocusManager.FocusedElement="{Binding txtLocation}" />
<Button IsDefault="True" Name="btnLoadForecast" Content="Load Weather Forecast" Command="Refresh" Margin="0,10,10,10" Padding="10"/>
</StackPanel>
</Grid>
</Window>