问题描述
我正在尝试制作一些 wpf,它显示某个日期范围内每一天的用户控件。到目前为止我尝试过datagrid,但它不支持不同的行数据类型,所以我试图通过UserControl和ItemsControl来解决它。
我的 UserControl - PeriodDayControl.xaml 如下所示:
<UserControl x:Class="WpfApp.PeriodDayControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfApp"
mc:Ignorable="d" Height="168.443" Width="104.098">
<Grid>
<TextBox Text="{Binding Path=name}" HorizontalAlignment="Left" Height="23" Margin="20,10,21,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="63" TextAlignment="Center"/>
<TextBox Text="{Binding Path=price}" HorizontalAlignment="Left" Height="23" Margin="31,61,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="41" TextAlignment="Center"/>
<CheckBox IsChecked="{Binding Path=check}" Content="CheckBox" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="42,117,0" Height="16" Width="16" RenderTransformOrigin="0.372,0.449"/>
</Grid>
</UserControl>
以及注册绑定属性的代码(PeriodDayControl.xaml.cs):
public PeriodDayControl()
{
InitializeComponent();
}
public string name
{
get { return (string)GetValue(nameProperty); }
set { SetValue(nameProperty,value); }
}
public static readonly DependencyProperty nameProperty =
DependencyProperty.Register("name",typeof(string),typeof(PeriodDayControl));
public decimal price
{
get { return (decimal)GetValue(priceProperty); }
set { SetValue(priceProperty,value); }
}
public static readonly DependencyProperty priceProperty =
DependencyProperty.Register("price",typeof(decimal),typeof(PeriodDayControl));
public bool check
{
get { return (bool)GetValue(checkProperty); }
set { SetValue(checkProperty,value); }
}
public static readonly DependencyProperty checkProperty =
DependencyProperty.Register("check",typeof(bool),typeof(PeriodDayControl));
在 MainWindow.xaml 中,我尝试使用 Items 控件来创建模板:
<ItemsControl x:Name="ItemsControlMain" ItemsSource="{Binding ViewModels}" Margin="0,166,0">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"></StackPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<local:PeriodDayControl name="{Binding Path=nameProperty}" price="{Binding Path=priceProperty}" check="{Binding Path=checkProperty}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
这是我尝试创建数据并将其填充到 PeriodDayControl:
List<PeriodDayControl> pdcList = new List<PeriodDayControl>();
pdcList.Add(new PeriodDayControl() { name = "01/01",price =(decimal)55,check=true});
pdcList.Add(new PeriodDayControl() { name = "02/01",check=false});
pdcList.Add(new PeriodDayControl() { name = "03/01",check=true});
ObservableCollection<PeriodDayControl> pdcColl = new ObservableCollection<PeriodDayControl>(pdcList);
ItemsControlMain.ItemsSource = pdcColl;
这是我的问题。使用 ItemsControl 是正确的决定吗?或者还有其他可能性来实现这种功能吗?这个解决方案有什么问题(为什么它不填充数据)?为了制作上下文,我想更改复选框/价格,然后按 button ,这将从所有 UserControl 读取数据并将其保存到 db。
感谢您的任何建议。>
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)