自定义 WPF 数据绑定:如何添加自定义逻辑?
我对一些复杂的数据绑定有疑问。
我希望能够更新网格(其属性“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Grid
用于布局。如果某些集合中的项目数量不断变化,那么您真正想要的是ItemsControl
或更具体的ListBox
(如果您想要项目选择等)。如果您仍然希望单个行具有类似
Grid
的行为,您可能需要在ItemsControl.ItemTemplate
中定义Grid
并使用Grid.IsSharedSizeScope
在ItemsControl
级别。或者,您可以只使用ListView
来获取包中的网格外观和项目选择。Grid
is used for layout. If you have a changing number of items in some collection, what you really want isItemsControl
, or more specificListBox
(if you want item selection, etc).If you still want
Grid
-like behavior of individual rows, you may want to define aGrid
inItemsControl.ItemTemplate
and play withGrid.IsSharedSizeScope
atItemsControl
level. Alternatively, you may just useListView
instead to get the grid look and item selection in a package.更新:我通过创建一个使用 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.