我的ViewModel中的一个属性是对象的集合。我无法弄清楚如何将我的内部DataTemplate绑定到集合中这些对象的属性:有问题的绑定在TestDataTemplate1中,它只是找不到N和STR ..这是我的XAML
<Page.Resources>
<DataTemplate x:Key="TestDataTemplate1">
<Grid Background="Cornsilk" HorizontalAlignment="Stretch" Height="Auto">
<TextBlock x:Name="LeftTB" Text="{Binding N}" Foreground="Red" HorizontalAlignment="Left" VerticalAlignment="Stretch"/>
<TextBlock x:Name="RightTB" Text="{Binding STR}" Foreground="Green" HorizontalAlignment="Right" VerticalAlignment="Stretch"/>
</Grid>
</DataTemplate>
<DataTemplate x:Key="TestDataTemplate">
<Grid Background="Yellow" HorizontalAlignment="Stretch" Height="Auto">
<TextBlock x:Name="LeftTB" Text="{Binding Name}" HorizontalAlignment="Left" VerticalAlignment="Center"/>
<ListBox x:Name="MyListBox" x:FieldModifier="Public"
ItemsSource="{Binding SampleCollection}"
ItemTemplate="{StaticResource TestDataTemplate1}"
Background="Blue"
HorizontalAlignment="Center"
VerticalAlignment="Stretch" Width="200">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
<TextBlock x:Name="RightTB" Text="{Binding Age}" HorizontalAlignment="Right" VerticalAlignment="Center"/>
</Grid>
</DataTemplate>
</Page.Resources>
这是我的Type包含以下属性:
class AlaBalaVM : INotifyPropertyChanged
{
AlaBala _ab = null;
public AlaBalaVM(AlaBala ab)
{
_ab = ab;
}
public string N
{
get
{
return "num: " + _ab._n.ToString();
}
set { RaisePropertyChanged("N"); }
}
public string STR
{
get
{
return _ab._str.ToString() + " string";
}
set { RaisePropertyChanged("STR"); }
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
这是我填充DataSource集合的ViewModel:
public class PersonVMWrapper : INotifyPropertyChanged
{
PersonModel _pm = null;
public PersonVMWrapper(PersonModel pm)
{
_pm = pm;
}
public string Name
{
get
{
return "mr." + _pm.Name;
}
set { RaisePropertyChanged("Name"); }
}
public string Age
{
get
{
return _pm.Age.ToString() + " years";
}
set { RaisePropertyChanged("Age"); }
}
public ObservableCollection<AlaBala> SampleCollection
{
get
{
return _pm.SampleCollection;
}
set { RaisePropertyChanged("SampleCollection"); }
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
public class PersonVM : INotifyPropertyChanged
{
DispatcherTimer _refreshTimer = new DispatcherTimer();
private ObservableCollection<PersonVMWrapper> personDataSource;
public PersonModel PM { get; set; }
public PersonVM()
{
AddItemToDataSource(null);
}
private void AddItemToDataSource(object o)
{
DataSource.Add(new PersonVMWrapper(new PersonModel() { Name = "asdasd", Age = 23, SampleCollection = new ObservableCollection<AlaBala> { new AlaBala(3, "e1"), new AlaBala(4, "e2") } }));
}
public ObservableCollection<PersonVMWrapper> DataSource
{
get
{
if (this.personDataSource == null)
{
this.personDataSource = new ObservableCollection<PersonVMWrapper>();
}
return this.personDataSource;
}
set
{
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
答案 0 :(得分:0)
您尚未发布“Alabala”类的定义 - 它是否包含“N”和“STR”属性?正如@KooKiz指出的那样,看起来你打算让“SampleCollection”成为ObservableCollection<AlaBalaVM>
,即:
SampleCollection = new ObservableCollection<AlaBalaVM>
{
new AlaBalaVM(new AlaBala(3, "e1")),
new AlaBalaVM(new AlaBala(4, "e2")),
}