我需要在另一个ObservableCollection中绑定一个ObservableCollection。 xaml如下。外部ItemControl工作正常,并且RangeLeft'财产显示正常。问题在于内部ItemControl。创建Wrap面板(在内部ItemControl中)的计数是根据内部列表中的项目,但属性' ContionalString'永远不会显示。
<ItemsControl ItemsSource="{Binding mMngModelList}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Expander>
<StackPanel >
<WrapPanel>
<TextBlock Text="{Binding RangeLeft}"/>
</WrapPanel>
<ItemsControl ItemsSource="{Binding ConditionList}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<WrapPanel>
<TextBlock Text="{Binding ConditionString}"/>
<TextBlock Text=" "/>
<Button Content="+" />
</WrapPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</Expander>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Code Behind是
public class ManagementFunctionModel : INotifyPropertyChanged, IDataErrorInfo
{
#region members
string _Type;
int _RangeLeft;
int _RangeTop;
int _RangeRight;
int _RangeBottom;
public ObservableCollection<Condition> _ConditionList { get; private set; }
#endregion
public ManagementFunctionModel()
{
_ConditionList = new ObservableCollection<Condition>();
_ConditionList.Add(new Condition() { ConditionString = "condition 1" });
_ConditionList.Add(new Condition() { ConditionString = "condition 2" });
_ConditionList.Add(new Condition() { ConditionString = "condition 3" });
}
public ObservableCollection<Condition> ConditionList
{
get { return _ConditionList; }
set
{
if (_ConditionList != value)
{
_ConditionList = value;
RaisePropertyChanged("ConditionList");
}
}
}
public int RangeLeft
{
get { return _RangeLeft; }
set
{
if (_RangeLeft != value)
{
_RangeLeft = value;
RaisePropertyChanged("RangeLeft");
}
}
}
条件类
public class Condition
{
public string ConditionString;
}
在我看来
mMngModelList = new ObservableCollection<ManagementFunctionModel>();
mMngModelListShow.Add(new ManagementFunctionModel() { RangeLeft = 9, RangeTop = 3 });
mMngModelListShow.Add(new ManagementFunctionModel() { RangeLeft = 10, RangeTop = 1 });
mMngModelListShow.Add(new ManagementFunctionModel() { RangeLeft = 11, RangeTop = 2 });
答案 0 :(得分:1)
我尝试了您的代码,如果您将条件类中的 ConditionString 声明为属性而不是经典字段,则可以正常工作。这是我的测试代码:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var mMngModelList = new ObservableCollection<ManagementFunctionModel>();
mMngModelList.Add(new ManagementFunctionModel() { RangeLeft = 9 });
mMngModelList.Add(new ManagementFunctionModel() { RangeLeft = 10 });
mMngModelList.Add(new ManagementFunctionModel() { RangeLeft = 11 });
this.DataContext = mMngModelList;
}
}
public class Condition
{
public string ConditionString { get; set; }
}
public class ManagementFunctionModel : INotifyPropertyChanged, IDataErrorInfo
{
#region members
string _Type;
int _RangeLeft;
int _RangeTop;
int _RangeRight;
int _RangeBottom;
public ObservableCollection<Condition> _ConditionList { get; private set; }
#endregion
public ManagementFunctionModel()
{
_ConditionList = new ObservableCollection<Condition>();
_ConditionList.Add(new Condition() { ConditionString = "condition 1" });
_ConditionList.Add(new Condition() { ConditionString = "condition 2" });
_ConditionList.Add(new Condition() { ConditionString = "condition 3" });
}
public ObservableCollection<Condition> ConditionList
{
get { return _ConditionList; }
set
{
if (_ConditionList != value)
{
_ConditionList = value;
RaisePropertyChanged("ConditionList");
}
}
}
public int RangeLeft
{
get { return _RangeLeft; }
set
{
if (_RangeLeft != value)
{
_RangeLeft = value;
RaisePropertyChanged("RangeLeft");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(String property)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
public string Error
{
get { throw new NotImplementedException(); }
}
public string this[string columnName]
{
get { throw new NotImplementedException(); }
}
}
这是UI代码:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ItemsControl ItemsSource="{Binding}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Expander>
<StackPanel >
<WrapPanel>
<TextBlock Text="{Binding RangeLeft}"/>
</WrapPanel>
<ItemsControl ItemsSource="{Binding ConditionList}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<WrapPanel>
<TextBlock Text="{Binding ConditionString}"/>
<TextBlock Text=" "/>
<Button Content="+" />
</WrapPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</Expander>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>