我有Outlook喜欢的应用程序。有3个部分。中间部分包含ListView。 中段ListView的元素已计算样式
(红色字体=过期作业,Bolt字体=未读作业,Strikeout text = 执行工作)
。条件可能会有不同的变化。
我有第2节的Xaml标记:
<HubSection Name="Section2" Width="400" DataContext="{Binding Section2Items}"
d:DataContext="{Binding Groups[0], Source={d:DesignData Source=../HopUp.Shared/DataModel/SampleData.json, Type=data:SampleDataSource}}"
x:Uid="Section2Header" Header="Section 2" Padding="5,25,0,10">
<DataTemplate>
<ListView
x:Name="lvSection2"
ItemsSource="{Binding Items}"
Margin="0,-0,0,0"
AutomationProperties.AutomationId="ItemGridView"
AutomationProperties.Name="Items In Group"
ItemTemplate="{StaticResource StandardTripleLineItemTemplate}"
SelectionMode="Single"
IsSwipeEnabled="false"
IsItemClickEnabled="True"
ItemClick="ItemView_ItemClickContent">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
</VisualStateGroup>
<VisualStateGroup x:Name="SelectionStates">
<VisualState x:Name="Unselected">
<Storyboard>
<ColorAnimation Duration="0" Storyboard.TargetName="myback" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" To="Transparent"/>
</Storyboard>
</VisualState>
<VisualState x:Name="SelectedUnfocused">
<Storyboard>
<ColorAnimation Duration="0" Storyboard.TargetName="myback" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" To="Red"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Border x:Name="myback" Background="Transparent">
<ContentPresenter Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}"/>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListView.ItemContainerStyle>
</ListView>
</DataTemplate>
</HubSection>
我需要根据不同的条件设置不同的ListViewItem样式。
背后有代码:
SampleDataGroup oContentFolder = await MainFunctionality.GetContent(pbActive, m_sUri, sFirstID, m_sSessionID);
if (oContentFolder != null)
{
Section2.Header = sFirstName;
this.DefaultViewModel["Section2Items"] = oContentFolder;
lv = Utilities.Utilities.FindVisualChildByName<ListView>(Section2, "lvSection2");
if (lv != null)
for (int j = 0; j < oContentFolder.Items.Count; j++)
{
if (oContentFolder.Items[j].ItemType == "ctJob")
{
if (oContentFolder.Items[j].ItemState == "InWork")
{
}
}
}
lv.SelectedIndex = 0;
如何设置ListViewItem样式
答案 0 :(得分:1)
我不是WinRT专家,但这可能有所帮助:
//The class to style
public class SampleTaskItem
{
public SampleTaskItem()
{
TaskId = (new Random()).Next();
}
//Your properties
public int TaskId { get; private set; }
//Calculate these with your object's data
public bool Done { get; set; }
public bool Late { get; set; }
public bool IsNew { get; set; }
}
public class TaskItemStyleConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is SampleTaskItem)
{
var myTask = value as SampleTaskItem;
var property = parameter.ToString().ToLower();
if (property == "fontweight")
{
return myTask.IsNew ? FontWeights.Bold : FontWeights.Normal;
}
if (property == "foreground")
{
return myTask.Late ? Brushes.Red : Brushes.Black;
}
if (property == "strike")
{
return myTask.Done ? TextDecorations.Strikethrough : null;
}
throw new NotImplementedException();
}
throw new NotImplementedException();
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
然后,您的ListView可能类似于:
<ListView ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type loc:StyledListview}}, Path=SampleTasks}">
<ListView.Resources>
<loc:TaskItemStyleConverter x:Key="STYLER"/>
</ListView.Resources>
<ListView.ItemTemplate>
<DataTemplate DataType="{x:Type loc:SampleTaskItem}">
<TextBlock Foreground="{Binding Converter={StaticResource STYLER}, ConverterParameter=foreground}"
FontWeight="{Binding Converter={StaticResource STYLER}, ConverterParameter=fontweight}"
TextDecorations="{Binding Converter={StaticResource STYLER}, ConverterParameter=strike}"
Text="{Binding TaskId}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
在SampleTaskItem
中检查的任何标记都会触发所需的样式,它们可以从无到有结合。