C# 从codebehind更改ItemsControl内容

C# 从codebehind更改ItemsControl内容,c#,wpf,C#,Wpf,我有wpf控制TimeLineControl和代码: ... <ItemsControl ItemsSource="{Binding TimeLineItems}" > <ItemsControl.ItemTemplate> <DataTemplate> <colorLine:TimeLineItem /> </DataTemplate> </ItemsCon

我有wpf控制
TimeLineControl
和代码:

...
<ItemsControl ItemsSource="{Binding TimeLineItems}" >
   <ItemsControl.ItemTemplate>
        <DataTemplate>
             <colorLine:TimeLineItem />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
    <ItemsControl.ItemsPanel>
         <ItemsPanelTemplate>
              <StackPanel Orientation="Horizontal"/>
         </ItemsPanelTemplate>
     </ItemsControl.ItemsPanel>
</ItemsControl>
...
我使用它来更改
ItemsControl
dynamicali中
TimeLineItems
的数量

但我想去掉这两个冗余视图模型(
TimeLineViewModel
TimeLineItemViewModel
),因为实际上我只使用它来控制1个值:
TimeLineItems
的数量-我知道这个逻辑在将来会持续存在,不会改变

因此,我想让我的
TimeLineControl
不带ViewModel,但在code behind中使用自定义
DependencyProperty
(我猜),它将具有类型
int
。我们将其称为AmountOfSectionsProperty。当
AmountOfSectionsProperty
将更改时,它会将
ItemsControl
中的
TimeLineItems
的数量更改为其值

我实现此属性的方式如下:

 public readonly static DependencyProperty AmountOfSectionsProperty = DependencyProperty.Register("AmountOfSections",
            typeof(int),
            typeof(TimeLineControl),
            new FrameworkPropertyMetadata(1, FrameworkPropertyMetadataOptions.AffectsParentMeasure, AmountOfSectionsChangedCallback, CoerceAmountOfSectionsCallback));

        private static object CoerceAmountOfSectionsCallback(DependencyObject dependencyObject, object baseValue)
        {
            var current = (int)baseValue;
            if (current < 1) current = 1;
            if (current > 24) current = 24;
            return current;
        }

        private static void AmountOfSectionsChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
        {
            throw new NotImplementedException();
        }

        public int AmountOfSections 
        {
            get { return (int)GetValue(AmountOfSectionsProperty); }
            set { SetValue(AmountOfSectionsProperty, value); }
        }
public只读静态DependencyProperty AmountOfSectionsProperty=DependencyProperty.Register(“AmountOfSections”,
类型(int),
类型(时间线控制),
新的FrameworkPropertyMetadata(1,FrameworkPropertyMetadata选项.AffectsParentMeasure,AmountOfSectionsChangedCallback,强制eamountofSectionsScallback));
私有静态对象强制eamountofSectionScallBack(DependencyObject DependencyObject,object baseValue)
{
var电流=(int)基值;
如果(电流<1)电流=1;
如果(电流>24)电流=24;
回流;
}
私有静态无效AmountOfSectionsChangedCallback(DependencyObject DependencyObject,DependencyPropertyChangedEventArgs DependencyPropertyChangedEventArgs DependencyChangedEventArgs)
{
抛出新的NotImplementedException();
}
公共int AmountOfSections
{
获取{return(int)GetValue(AmountOfSectionsProperty);}
set{SetValue(AmountOfSectionsProperty,value);}
}

它应该在1..24范围内。但我有点坚持实际绑定-我的回调方法应该如何,以更改
ItemsControl
items的数量?

但我有点坚持实际绑定-我的回调方法应该如何,以更改ItemsControl项目的数量?。。。您真正想要的是什么?@Sheridan i-want:i将int variable绑定到TimeLineControl属性,并将其自身items控件中的项数与此变量一起更改。
 public readonly static DependencyProperty AmountOfSectionsProperty = DependencyProperty.Register("AmountOfSections",
            typeof(int),
            typeof(TimeLineControl),
            new FrameworkPropertyMetadata(1, FrameworkPropertyMetadataOptions.AffectsParentMeasure, AmountOfSectionsChangedCallback, CoerceAmountOfSectionsCallback));

        private static object CoerceAmountOfSectionsCallback(DependencyObject dependencyObject, object baseValue)
        {
            var current = (int)baseValue;
            if (current < 1) current = 1;
            if (current > 24) current = 24;
            return current;
        }

        private static void AmountOfSectionsChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
        {
            throw new NotImplementedException();
        }

        public int AmountOfSections 
        {
            get { return (int)GetValue(AmountOfSectionsProperty); }
            set { SetValue(AmountOfSectionsProperty, value); }
        }