WPF到Silverlight ItemContainerStyle的问题 [英] WPF to Silverlight ItemContainerStyle problem

查看:72
本文介绍了WPF到Silverlight ItemContainerStyle的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。在尝试从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 =Horizo​​ntalAlignmentValue =Stretch/>
< Setter Property =VerticalAlignmentValue =Stretch/>
< Setter Property =Horizo​​ntalContentAlignmentValue =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 }
Horizo​​ntalAlignment ={ TemplateBinding Horizo​​ntalContentAlignment }
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 for Style.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 no ItemContainerStyle property on the ItemsControl. It exists on derived controls such as ListBox. If you don''t need some ListBox-specific behavior, such as selection, use simplified ListBoxItem 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆