2

XAML

<Window x:Class="Html5Mapper.Mapper.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:wpt="http://schemas.xceed.com/wpf/xaml/toolkit"
    Title="HTML mapper" Height="300" Width="600" >
<Window.DataContext>
    <Binding Path="Main" Source="{StaticResource Locator}"/>
</Window.DataContext>

<ListBox Name="lbFiles" ItemsSource="{Binding Filescollection, Mode=OneWay}" Width="240" Height="220">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid Margin="0,2">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="100" />
                    </Grid.ColumnDefinitions>
                    <Image Source="HTML.png" Height="40" Width="32" />
                    <TextBlock Grid.Column="1" Text="{Binding Name}" />
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

主视图模型.cs

public ObservableCollection<Files> Filescollection { get; set; }    
public MainViewModel()
{
    this.Filescollection = new ObservableCollection<Files>();
    SelectFilesAction();
}

private void SelectFilesAction()
{
   this.Filescollection.Add(new Files { Name = "index.html", Path = "C:\\Files"});
   //lbFiles.ItemsSource = docs;
}

问:我还需要什么才能将docs对象放入 Listbox ?

4

3 回答 3

5

在我看来,您将控件绑定到错误的数据连接,请检查输出窗口是否有错误。您可能希望将主窗口的数据上下文设置为 MainViewModel(在代码隐藏或类似中)。另外,您为什么要为文档创建另一个实例?这是没用的。

public ObservableCollection<Files> Filescollection {get;set;}

public MainViewModel()
{
    this.Filescollection = new ObservableCollection<Files>();
    SelectFilesAction();
}

private void SelectFilesAction()
{
   Filescollection.Add(new Files { Name = "index.html", Path = "C:\\Files"});
}
于 2013-08-08T07:43:00.063 回答
2

Vidas 是正确的,因为您的 Window 的 DataContext 是错误的,它需要是您的 MainViewModel 类。

摆脱这个:

<Window.DataContext>
    <Binding Path="Main" Source="{StaticResource Locator}"/>
</Window.DataContext>

并将其添加到 Window 标签:

<Window DataContext="{StaticResource MainViewModel}">

那应该这样做。

于 2013-08-08T10:37:33.870 回答
2
<UserControl x:Class="RetailPOS.View.Usercontrols.MainWindow.Products"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
             mc:Ignorable="d" 
             xmlns:ctrl="clr-namespace:RetailPOS.View.Usercontrols.MainWindow"
             d:DesignHeight="200" d:DesignWidth="490" **DataContext="{Binding Source={StaticResource Locator}, Path=CategoryVM}"**
             xmlns:my="clr-namespace:WpfLab.Controls;assembly=WpfLab.Controls">
    <UserControl.Resources>
    </UserControl.Resources>
    <Grid Width="490" Height="360">
        <ListBox Name="LstProduct" ItemsSource="{Binding LstProduct}" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel Orientation="Horizontal" Margin="0" Height="Auto" Width="490" >
                    </WrapPanel>
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Button Margin="1" Content="{Binding Name}" Height="53" Background="{Binding Color}" HorizontalAlignment="Right" Width="79" 
                            Command="{Binding DataContext.ShowProductCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}}" 
                            CommandParameter="{Binding}">
                    </Button>                 
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

    </Grid>
</UserControl>

在后面的代码中

   private ObservableCollection<ProductDTO> _lstProduct;
   public ObservableCollection<ProductDTO> LstProduct
        {
            get { return _lstProduct; }
            set
            {
                _lstProduct = value;
                RaisePropertyChanged("LstProduct");
            }
        }

   /// <summary>
    /// Get all Commonly Used Products
    /// </summary>
    /// <returns>returns list of all Commonly Used  products present in database</returns>
private void FillCommonProducts()
{
    LstProduct = new ObservableCollection<ProductDTO>(from item in ServiceFactory.ServiceClient.GetCommonProduct() 
                               select item);
}
于 2013-09-15T06:25:00.890 回答