Windows phone 7 WP7从ListBoxItem样式绑定到listbox itemsource属性以外的其他属性
我在viewmodel中有如下属性:Windows phone 7 WP7从ListBoxItem样式绑定到listbox itemsource属性以外的其他属性,windows-phone-7,xaml,binding,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"> ...
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>
公共类TestObject
{
公共颜色{get;set;}
公共IList{get;set;}
...
}
视图中的列表框如下所示:
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>
我不确定你问题的控制风格部分。。。但是一般来说,如果我想绑定到当前项之外的内容,那么我会使用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中直接包含颜色,而是使用以下转换器:
<TextBlock Text="{Binding Title}" Foreground="{Binding DataContext.Priority, ElementName=RootLayout, Converter={StaticResource PriorityToColorConverter}}" />
如果您正在进行跨平台的viewmodels,这将特别有用,因为颜色和笔刷类在WP7和WinRT之间没有共享,更不用说MonoTouch和MonoDroid了。这可能是不可能的吗?那么另一个流程是:如何定制listbox的选定项?