【问题标题】:Custom Objects in Custom List WPF自定义列表 WPF 中的自定义对象
【发布时间】:2014-12-18 20:22:17
【问题描述】:

我有一个名为日历的视图模型。

在 Calendar 中有一个 CalendarDaySquare 对象列表。它们显示在一个 ItemsControl 中,它是一个统一网格(来自 ItemsPanelTemplate)。

在每个 calendarDaySquares 中,我想用事件集合(CalEvent 对象)填充它

public Class Calendar

{

// this is just a list that inherits from List<T> 
      public CalendarSquareList<CalendarDaySquare> Squares { get; private set; }

}

public class CalendarDaySquare
{

        public List<CalEvent> Events {get; private set;}

        public ObservableCollection<string> ObsColEvents{get; private set;}
}

是同样的事情,我只是尝试使用 ObservableCollection b/c 我一直被这个难住了。

这是我的 xaml:

<ItemsControl ItemsSource="{Binding Squares}"
                Margin="0,0,0,263">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>            
                        <UniformGrid Columns="7" Rows="5"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>

                <ItemsControl.ItemContainerStyle>
                <Style>
                    <Setter Property="Control.Margin" Value="1"/>
                </Style>
                </ItemsControl.ItemContainerStyle>

                <ItemsControl.ItemTemplate>
                    <DataTemplate>


                                        <ListBox ItemsSource="{Binding}" Margin="5">
                                            <ListBox.ItemTemplate>
                                                <DataTemplate xmlns:local="clr-namespace:BudgetCalendarWPF.ViewModel;assembly=BudgetCalendarWPF" DataType="CalendarDaySquare">
                                                                <StackPanel Orientation="Horizontal">
                                                                    <TextBlock Text="{Binding}"/>
                                                                </StackPanel>
                                                </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>

                                <!--</Button>
                                </ControlTemplate>
                            </Button.Template>
                        </Button>-->



                    </DataTemplate>

                </ItemsControl.ItemTemplate>
            </ItemsControl>

我希望我已经粘贴了最新的(我整天都在研究这个,所以这正是我目前在 VS 中得到的......悲伤的脸)

【问题讨论】:

    标签: wpf xaml custom-collection


    【解决方案1】:

    Welp,我想我已经想通了,但我喜欢任何建议。

    我很努力。我没有意识到它会自动拾取该项目的类型。所以这有效:

    <ListBox ItemsSource="{Binding Events}">
                                <ListBox.ItemTemplate>
                                    <DataTemplate >
                                        <Button HorizontalAlignment="Stretch">
                                            <TextBlock Text="{Binding Name}"/>
                                        </Button>
                                    </DataTemplate>
                                </ListBox.ItemTemplate>
                            </ListBox>
    

    【讨论】:

    • 是的,这就是数据绑定的美妙之处,对象的类型(实际上是对象本身)被传递到模板中,因此您可以做非常巧妙的绑定技巧。
    猜你喜欢
    • 1970-01-01
    • 2020-11-26
    • 2015-05-08
    • 1970-01-01
    • 2017-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多