user2837961
user2837961

Reputation: 1555

ItemControl Binding inside another ItemControl

I need to bind a ObservableCollection inside another ObservableCollection. The xaml is as below. The outer ItemControl works fine and the 'RangeLeft' property is displayed fine. The problem is with the Inner ItemControl. The count of Wrap Panels(in the inner ItemControl) are created is according to the items in the inner list but the property 'ContionalString' is never displayed.

 <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 is

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");
        }
    }
}

Condition Class

public class Condition
{
    public string ConditionString;
}

In my view

    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 });

Upvotes: 0

Views: 81

Answers (1)

FloChanz
FloChanz

Reputation: 3429

I tried your code and if if you declare the ConditionString in the Condition class as a property instead of a classic field it works fine. This is my test code :

    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(); }
    }
}

And this is the UI code :

<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>

Upvotes: 1

Related Questions