ItemsControl 在另一个 ItemsControl 中绑定
ItemControl Binding inside another ItemControl
我需要在另一个 ObservableCollection 中绑定一个 ObservableCollection。 xaml 如下。外部 ItemControl 工作正常并且 'RangeLeft' 属性 显示正常。问题出在内部 ItemControl。根据内部列表中的项目创建环绕面板(在内部 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>
后面的代码是
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");
}
}
}
条件Class
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 });
我试过你的代码,如果你在 Condition class 中将 ConditionString 声明为 属性 而不是 classic 字段,它工作正常。这是我的测试代码:
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>
我需要在另一个 ObservableCollection 中绑定一个 ObservableCollection。 xaml 如下。外部 ItemControl 工作正常并且 'RangeLeft' 属性 显示正常。问题出在内部 ItemControl。根据内部列表中的项目创建环绕面板(在内部 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>
后面的代码是
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");
}
}
}
条件Class
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 });
我试过你的代码,如果你在 Condition class 中将 ConditionString 声明为 属性 而不是 classic 字段,它工作正常。这是我的测试代码:
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>