仅在设计模式下填充的列表框-Silverlight
本文关键字:列表 -Silverlight 填充 设计模式 | 更新日期: 2023-09-27 18:00:48
它只在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等;Set;'s
我看到了我的数据!IsInDesignMode测试,我在其中填充它,但当单击cmdSubmit按钮时,它不会在UI中更新,即使我看到RaisePropertyChanged((正在激发。
有什么想法吗?
编辑:
添加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}}">
如何以及在哪里设置DataContext
?
尝试将BindingMode
设置为TwoWay
<ListBox Margin="5" x:Name="RemoveLookup" ItemsSource="{Binding Path=LocationObjectResults Mode=TwoWay}">