WPF到Silverlight ItemContainerStyle的问题 [英] WPF to Silverlight ItemContainerStyle problem
问题描述
您好。在尝试从WPF中移植一些代码时,我正在努力解决Silverlight中的问题。我的ViewModel中有一个名为Fields的ObservableCollection,它绑定到ItemsControl的ItemsSource。 ItemsPanelTemplate设置为通过辅助类绑定的网格。问题在于ItemContainerStyle,它在Silverlight(5)中不起作用。
< ItemsControl ItemsSource = {Binding Path = Fields} >
< ItemsControl.ItemsPanel >
< ItemsPanelTemplate >
< 网格 local:GridHelper.RowCount = {Binding Path = RowCount} local:GridHelper.ColumnCount = {Binding Path = ColumnCount} / >
< / ItemsPanelTemplate >
< / ItemsControl.ItemsPanel >
< ItemsControl.ItemContainerStyle >
< 样式 TargetType = {x :类型FrameworkElement} >
< Setter 属性 = Grid.Row 值 = {Binding Row} / >
< Setter 属性 = Grid.Column跨度> <温泉n class =code-attribute> 值 = {Binding Column} / >
< / Style >
< / ItemsControl.ItemContainerStyle >
< / ItemsControl >
我是Silverlight的新手,希望这不是一个愚蠢的问题:)提前谢谢!
1。 Silverlight对Style.TargetType
属性有不同的语法,使用
< style targettype = FrameworkElement > < / style >( Style.TargetType Property )。
2.在Silverlight 5中添加了属性设置器中的绑定。因此,如果使用SL4,它将无法正常工作。
3.ItemsControl
上没有ItemContainerStyle
属性。它存在于派生控件上,例如ListBox
。如果您不需要某些特定于ListBox的行为(例如选择),请使用简化的ListBoxItem
控件模板,例如:
< 样式 TargetType = ListBoxItem >
< Setter Property =Grid.RowValue ={绑定 行}/>
< Setter Property =Grid.ColumnValue ={ Binding 列跨度>}/>
< Setter Property =HorizontalAlignmentValue =Stretch/>
< Setter Property =VerticalAlignmentValue =Stretch/>
< Setter Property =HorizontalContentAlignmentValue =Stretch/>
< Setter Property =VerticalContentAlignmentValue =Stretch/>
< Setter Property =Template>
< Setter .Value>
< ControlTemplate TargetType =ListBoxItem>
< Grid Background ={ TemplateBinding 背景跨度>}>
< ContentPresenter x:Name =contentPresenter
Content ={ TemplateBinding 内容}
ContentTemplate ={ TemplateBinding ContentTemplate }
HorizontalAlignment ={ TemplateBinding HorizontalContentAlignment }
Margin ={{span class =code-attribute> TemplateBinding 填充}/>
< / 网格 >
< / ControlTemplate >
< / Setter.Value >
< / Setter >
< / Style > 跨度>
Hi. I am struggling with something in silverlight while trying to port some code from WPF. I have an ObservableCollection in my ViewModel called Fields, which is bound to the ItemsSource of an ItemsControl. The ItemsPanelTemplate is set to a grid with bindings through a helper class. The problem is with the ItemContainerStyle, it does not work in Silverlight (5).
<ItemsControl ItemsSource="{Binding Path=Fields}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid local:GridHelper.RowCount="{Binding Path=RowCount}" local:GridHelper.ColumnCount="{Binding Path=ColumnCount}"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="{x:Type FrameworkElement}">
<Setter Property="Grid.Row" Value="{Binding Row}"/>
<Setter Property="Grid.Column" Value="{Binding Column}"/>
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
I am new to Silverlight, hope it is not a dumb question :) Thanks in advance!
1. Silverlight has different syntax forStyle.TargetType
property, use
<style targettype="FrameworkElement"></style>(Style.TargetType Property).
2. Binding in the property setters has been added in Silverlight 5. So if you use SL4, it won''t work.
3. There is noItemContainerStyle
property on theItemsControl
. It exists on derived controls such asListBox
. If you don''t need some ListBox-specific behavior, such as selection, use simplifiedListBoxItem
control template, for example:
<Style TargetType="ListBoxItem"> <Setter Property="Grid.Row" Value="{Binding Row}"/> <Setter Property="Grid.Column" Value="{Binding Column}"/> <Setter Property="HorizontalAlignment" Value="Stretch"/> <Setter Property="VerticalAlignment" Value="Stretch"/> <Setter Property="HorizontalContentAlignment" Value="Stretch"/> <Setter Property="VerticalContentAlignment" Value="Stretch"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ListBoxItem"> <Grid Background="{TemplateBinding Background}"> <ContentPresenter x:Name="contentPresenter" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}"/> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style>
这篇关于WPF到Silverlight ItemContainerStyle的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!