簡體   English   中英

ListBox.ItemsSource {Binding…}不起作用,但是如果我在代碼中設置ItemsSource,那就太花哨了!

[英]ListBox.ItemsSource {Binding …} doesn't work, but if I set the ItemsSource in code, it's all dandy!

我正在使用WPF進行實驗,但是遇到了一個似乎無法弄清的問題,盡管我之前已經使用Silverlight做過一些工作,並且成功使用了DataContexts,Data Bindings和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();
}

當我像這樣運行應用程序時,一切都很好,主窗口上的列表框會像應顯示的那樣顯示在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>

除非ListBox在另一個DataContext中,否則您的代碼應該可以工作。

嘗試

<ListBox x:Name="weatherList" 
    Grid.Row="0" 
    ItemContainerStyle="{StaticResource stretched}" 
    ItemsSource="{Binding DataContext.WeatherInfo, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type yournamespace:MainWindow}}}" />

找出答案。

謝謝大家的寶貴建議。 我不知道在“輸出”窗口中顯示的“綁定錯誤”,從那里到解決方案只有一個Google。

我遇到的問題是我試圖綁定的WeatherInfo項目源是一個公共字段,而不是一個屬性。 因此, 我可以簡單地將WeatherInfo公共ObservableCollection分配給列表框的itemssource,但是我不能依靠數據綁定機制在DataContext中定位WeatherInfo屬性,因為WeatherInfo從技術上來說不是屬性 添加{get; 我的MainVM中的WeatherInfo聲明使數據綁定按預期方式工作,並且我不必再在代碼中將WeatherInfo對象分配為列表框的ItemsSource。

[編輯:覺得愚蠢]

好的,對不起,我看到您處於啟動狀態……在...之前快速回答... MainWindow ListBox所在的對象正確嗎?

但是,我將檢查以確保沒有更接近ListBox的對象(直接父對象等)擁有自己的DataContext。

  1. 加載表單時,請檢查輸出窗口以查看是否存在任何綁定錯誤消息。

  2. 一種確保您的DataContext正確的好方法是在ListBox旁邊拋出一個按鈕,在Window的代碼背后將事件鏈接到它,並放置一個斷點。 然后,當您到達所述斷點時,轉到您的直接窗口並通過將其轉換為ViewModel來窺視您的DataContext: (MainVM)DataContext

  3. WeatherInfo是靜態的嗎?

  4. 您可以發布MainVM目標代碼嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM