WP4从ListBox Item样式绑定到listbox itemssource属性以外的其他样式

时间:2012-07-26 12:27:43

标签: windows-phone-7 xaml binding

我在viewmodel中有这样的属性:

public class TestObject
{
    public Color Color { get; set; }
    public IList<Items> { get; set; }
    ...
}
像这样的

列表框:

<ListBox ItemsSource="{Binding TestObject.Items}" ....

和这样的列表框样式:

<Style TargetType="ListBoxItem">
           ...
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBoxItem">
                        <Border x:Name="LayoutRoot" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" VerticalAlignment="{TemplateBinding VerticalAlignment}">
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="SelectionStates">
                                    <VisualState x:Name="Unselected"/>
                                    <VisualState x:Name="Selected">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentContainer" Storyboard.TargetProperty="Foreground" Duration="0">
                                                <DiscreteObjectKeyFrame  KeyTime="0">
                                                    <DiscreteObjectKeyFrame.Value>
                                                        <SolidColorBrush Color="{**Binding to TestObject.Color ???}**" />
                                                    </DiscreteObjectKeyFrame.Value>
                                                </DiscreteObjectKeyFrame>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Border" Storyboard.TargetProperty="Background" Duration="0">
                                                <DiscreteObjectKeyFrame  KeyTime="0">
                                                    <DiscreteObjectKeyFrame.Value>
                                                        <SolidColorBrush Color="{**Binding to TestObject.Color ???}**"/>
                                                    </DiscreteObjectKeyFrame.Value>
                                                </DiscreteObjectKeyFrame>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>

...

那么请...如何绑定ItemsSource绑定?

1 个答案:

答案 0 :(得分:0)

我不确定你问题的控件风格部分...但是通常如果我想绑定到当前项之外的某些东西,那么我使用ElementName技巧使其工作 - 例如

 <Grid Name='RootLayout'>
     <ListBox ItemsSource='{Binding Items}'>
          <ListBox.ItemTemplate>
               <DataTemplate>
                    <TextBlock Text="{Binding Title}" Foreground="{Binding DataContext.PriorityColor, ElementName=RootLayout}" />
               </DataTemplate>
          </ListBox.ItemTemplate>
     </ListBox>
 </Grid>

我通常也不会直接在我的ViewModel中包含Color,而是使用类似的转换器:

 <TextBlock Text="{Binding Title}" Foreground="{Binding DataContext.Priority, ElementName=RootLayout, Converter={StaticResource PriorityToColorConverter}}" />

如果您正在进行跨平台视图模型,这将特别有用 - 因为不在WP7和WinRT之间共享Color和Brush类,更不用说在MonoTouch和MonoDroid中。