列表框仅在设计模式下填充 - Silverlight

时间:2011-06-30 01:44:10

标签: c# silverlight mvvm listbox

它仅显示VS2010中的数据,而不是在运行时。

        <ListBox Margin="5" x:Name="RemoveLookup" ItemsSource="{Binding Path=LocationObjectResults}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Grid Height="60" Loaded="Grid_Loaded">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="20*"/>
                                <ColumnDefinition Width="12*"/>
                                <ColumnDefinition Width="12*"/>
                                <ColumnDefinition Width="12*"/>
                                <ColumnDefinition Width="12*"/>
                                <ColumnDefinition Width="10*"/>
                                <ColumnDefinition Width="10*"/>
                            </Grid.ColumnDefinitions>
                            <Border x:Name="lblID" Grid.Column="0" Style="{StaticResource CustomDisplayBorder}">
                                <TextBlock Style="{StaticResource CustomDisplayText}" Text="{Binding Path=ID}" />
                            </Border>
                            <Border Name="lblLocation" Grid.Column="1" Style="{StaticResource CustomDisplayBorder}">
                                <TextBlock Text="{Binding Path=Location}" Style="{StaticResource CustomDisplayText}"/>
                            </Border>
                            <Border Name="lblItemNum" Grid.Column="2" Style="{StaticResource CustomDisplayBorder}">
                                <TextBlock Text="{Binding Path=ItemNum}" Style="{StaticResource CustomDisplayText}"/>
                            </Border>
                            <Border Name="lblQuantity" Grid.Column="3" Style="{StaticResource CustomDisplayBorder}">
                                <TextBlock Text="{Binding Path=LotCode}" Style="{StaticResource CustomDisplayText}"/>
                            </Border>
                            <Border Name="lblLotCode" Grid.Column="4" Style="{StaticResource CustomDisplayBorder}">
                                <TextBlock Text="{Binding Path=Quantity}" Style="{StaticResource CustomDisplayText}"/>
                            </Border>
                            <Border Name="lblFillDate" Grid.Column="5" Style="{StaticResource CustomDisplayBorder}">
                                <TextBlock Text="{Binding Path=FillDate}" Style="{StaticResource CustomDisplayText}"/>
                            </Border>
                        </Grid>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

ViewModel -

        public RemoveViewModel()
    {
        if (!IsInDesignMode)
        {

        }
        else
        {
            _locationObjectResults.Add(new LocationObject()
                {
                    ID = "test",
                    Location = "test2",
                    ItemNum = "123123",
                    LotCode = "123123123",
                    Quantity = "500",
                    FillDate = DateTime.Now
                });
        }
    }

    public ObservableCollection<LocationObject> LocationObjectResults
    {
        get
        {
            return this._locationObjectResults;
        }
        set
        {
                this._locationObjectResults = value;
                base.RaisePropertyChanged(() => this.LocationObjectResults);
        }
    }

    public void PopulateLocationObjects()
    {
        //var itemList = new ObservableCollection<LocationObject>()
        //    {
        //        new LocationObject("test1","test2","test3","500","123123",DateTime.Now)
        //    };
        _locationObjectResults.Add(new LocationObject()
            {                        ID = "test",
                    Location = "test2",
                    ItemNum = "123123",
                    LotCode = "123123123",
                    Quantity = "500",
                    FillDate = DateTime.Now
            });
        base.RaisePropertyChanged(() => this.LocationObjectResults);
    }

ViewModel正在查看一个名为LocationObject的类,其中包含ID,Location等...标准Get; Set;'s

我看到来自my!IsInDesignMode测试的数据,我在其中填充了它,但是当单击cmdSubmit按钮时,即使我看到RaisePropertyChanged()触发,它也不会在UI中更新。

有什么想法吗?

编辑:

为ViewModelBase添加代码 -

        private static bool? isInDesignMode;

    public bool IsInDesignMode
    {
        get
        {
            if (!isInDesignMode.HasValue)
            {
                isInDesignMode = DesignerProperties.IsInDesignTool;
            }
            return isInDesignMode.Value;
        }
    }

    protected void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpression)
    {
        if (propertyExpression.Body.NodeType == ExpressionType.MemberAccess)
        {
            var memberExpr = propertyExpression.Body as MemberExpression;
            string propertyName = memberExpr.Member.Name;
            this.OnPropertyChanged(propertyName);
        }
    }

这是来自父网格的代码

    <UserControl.Resources>
    <viewModels:RemoveViewModel x:Key="ViewModel" />        
</UserControl.Resources>

<Grid x:Name="LayoutRoot" Loaded="LayoutRoot_Loaded" DataContext="{Binding Source={StaticResource ViewModel}}">

1 个答案:

答案 0 :(得分:0)

您如何以及在何处设置DataContext

尝试将BindingMode设置为TwoWay

<ListBox Margin="5" x:Name="RemoveLookup" ItemsSource="{Binding Path=LocationObjectResults Mode=TwoWay}">