wp - ListBox选用不同的模板
2012-11-06 16:25:46 来源:WEB开发网核心提示:第五步:打开xaml,在刚才添加的MyListBoxItemStyle中找到视觉状态组SelectionStates,wp - ListBox选用不同的模板(2),在它的下面有一个Selected状态,在这个状态下面有一段代码:<Storyboard><ObjectAnimationUsingKeyF
第五步:打开xaml。在刚才添加的MyListBoxItemStyle中找到视觉状态组SelectionStates。在它的下面有一个Selected状态。在这个状态下面有一段代码:
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneAccentBrush}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
这是一段动画。代码的意思是将ListBox的选中项(ContentContainer)的前景色(Foreground)换成红色(Value="{StaticResource PhoneAccentBrush}")。
同样的原理,在这段动画再加一段动画,把选中项的模板换掉。
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="ContentTemplate" Storyboard.TargetName="ContentContainer">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SelectDataTemplate}"></DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
第六步:绑定数据。
public class Consumption
{
//消费名称
public string ConsumptionName { get; set; }
//消费金额
public float ConsumptionAmount { get; set; }
}
***********************************************************
List<Consumption> consumptionList = new List<Consumption> {
new Consumption{ConsumptionName="早餐", ConsumptionAmount=2},
new Consumption{ConsumptionName="午餐", ConsumptionAmount=12},
new Consumption{ConsumptionName="晚餐", ConsumptionAmount=8},
new Consumption{ConsumptionName="水果", ConsumptionAmount=6},
new Consumption{ConsumptionName="杂志", ConsumptionAmount=6}
};
focusListBox.ItemsSource = consumptionList;
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneAccentBrush}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
这是一段动画。代码的意思是将ListBox的选中项(ContentContainer)的前景色(Foreground)换成红色(Value="{StaticResource PhoneAccentBrush}")。
同样的原理,在这段动画再加一段动画,把选中项的模板换掉。
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="ContentTemplate" Storyboard.TargetName="ContentContainer">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SelectDataTemplate}"></DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
第六步:绑定数据。
public class Consumption
{
//消费名称
public string ConsumptionName { get; set; }
//消费金额
public float ConsumptionAmount { get; set; }
}
***********************************************************
List<Consumption> consumptionList = new List<Consumption> {
new Consumption{ConsumptionName="早餐", ConsumptionAmount=2},
new Consumption{ConsumptionName="午餐", ConsumptionAmount=12},
new Consumption{ConsumptionName="晚餐", ConsumptionAmount=8},
new Consumption{ConsumptionName="水果", ConsumptionAmount=6},
new Consumption{ConsumptionName="杂志", ConsumptionAmount=6}
};
focusListBox.ItemsSource = consumptionList;
更多精彩
赞助商链接