自定义 WPF 数据绑定:如何添加自定义逻辑?

发布于 2024-08-30 21:57:46 字数 1980 浏览 3 评论 0原文

我对一些复杂的数据绑定有疑问。
我希望能够更新网格(其属性“IsItemsHost”设置为 true)
每当发生数据绑定时都是动态的。
实际上我正在使用一个 CustomControl,它是一个 ItemsControl,这个
它的 ControlTemplate 中有 Grid。

更具体地说,我将网格绑定到一些项目,我想 根据这些项目更改网格行数, 添加诸如标题之类的内容(包含一些文本的一行), 并使用一些自定义逻辑设置项目的 Grid.Row 和 Grid.Column。

应用这种行为的最简单方法是什么 绑定数据何时更新?

我是否必须使用也包含标题数据的视图模型?

提前致谢。

CustomControl Generic.xaml 的代码:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TimeTableControl">
<Style TargetType="{x:Type local:TimeTableControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:TimeTableControl}">
                <Border Width="Auto" Height="Auto" BorderBrush="#FF4B5A9B" BorderThickness="4" CornerRadius="4" Margin="2" Padding="0" Background="White">
                    <Grid Width="Auto">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="0.1*"/>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>
                        <Viewbox>
                            <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=DayCaption}"/>
                        </Viewbox>
                        <Border Grid.Row="1" BorderThickness="0,2,0,0" BorderBrush="#FF4B5A9B">
                            <Grid Name="ContentGrid" IsItemsHost="True">
                            </Grid>
                        </Border>
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
</ResourceDictionary>

i have a question regarding some complex data-binding.
I want to be able to update a grid (which has the property "IsItemsHost" set to true)
dynamically whenever a data-binding occurs.
Actually i am using a CustomControl which is an ItemsControl and this
has the Grid in its ControlTemplate.

To be more specific, i bind the grid to some items and i want to
change the number of grid rows depending on these items,
add something like a header (one row containing some text),
and set the items' Grid.Row and Grid.Column using some custom logic.

What is the easiest way to apply such behaviour
whenever the bound data is updated?

Do i have to use a viewmodel that also contains the header data?

Thanks in advance.

Code of the CustomControl Generic.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TimeTableControl">
<Style TargetType="{x:Type local:TimeTableControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:TimeTableControl}">
                <Border Width="Auto" Height="Auto" BorderBrush="#FF4B5A9B" BorderThickness="4" CornerRadius="4" Margin="2" Padding="0" Background="White">
                    <Grid Width="Auto">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="0.1*"/>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>
                        <Viewbox>
                            <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=DayCaption}"/>
                        </Viewbox>
                        <Border Grid.Row="1" BorderThickness="0,2,0,0" BorderBrush="#FF4B5A9B">
                            <Grid Name="ContentGrid" IsItemsHost="True">
                            </Grid>
                        </Border>
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
</ResourceDictionary>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无悔心 2024-09-06 21:57:46

Grid 用于布局。如果某些集合中的项目数量不断变化,那么您真正想要的是 ItemsControl 或更具体的 ListBox (如果您想要项目选择等)。

如果您仍然希望单个行具有类似 Grid 的行为,您可能需要在 ItemsControl.ItemTemplate 中定义 Grid 并使用 Grid.IsSharedSizeScopeItemsControl 级别。或者,您可以只使用 ListView 来获取包中的网格外观和项目选择。

Grid is used for layout. If you have a changing number of items in some collection, what you really want is ItemsControl, or more specific ListBox (if you want item selection, etc).

If you still want Grid-like behavior of individual rows, you may want to define a Grid in ItemsControl.ItemTemplate and play with Grid.IsSharedSizeScope at ItemsControl level. Alternatively, you may just use ListView instead to get the grid look and item selection in a package.

满栀 2024-09-06 21:57:46

更新:我通过创建一个使用 MeasureOverride 和 ArrangeOverride 来更新自身的自定义面板来使其工作。这让我可以根据子项调整面板,甚至不需要使用网格。这也使得控件看起来毫无意义。

Update: I got it working by creating a custom panel that uses MeasureOverride and ArrangeOverride to update itself. This lets me adjust the panel to the children and I don't even need to use a grid. This also makes the control lookless.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文