C# WinRT Xaml为ListViewItems设置不同的样式
我已经申请了Outlook。共有3个部分。中间部分包含ListView。 中间部分ListView的元素具有计算样式 (红色字体=过期作业,螺栓字体=未读作业,删除文本= 执行的工作) 。条件可能以不同的形式相交 我有第2节的Xaml标记:C# WinRT Xaml为ListViewItems设置不同的样式,c#,wpf,xaml,windows-runtime,C#,Wpf,Xaml,Windows Runtime,我已经申请了Outlook。共有3个部分。中间部分包含ListView。 中间部分ListView的元素具有计算样式 (红色字体=过期作业,螺栓字体=未读作业,删除文本= 执行的工作) 。条件可能以不同的形式相交 我有第2节的Xaml标记: <HubSection Name="Section2" Width="400" DataContext="{Binding Section2Items}" d:DataContext="{Bindi
<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;
SampleDataGroup oContentFolder=wait main functional.GetContent(pbActive、m_sUri、sFirstID、m_sSessionID);
如果(oContentFolder!=null)
{
第2节标题=sFirstName;
此.DefaultViewModel[“Section2Items”]=oContentFolder;
lv=Utilities.Utilities.FindVisualChildByName(第2节,“lvSection2”);
如果(lv!=null)
对于(int j=0;j
如何设置ListViewItem样式我不是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
中选中的任何标志都将触发所需的样式,它们可以从无到全部进行组合。我认为必须通过对lv.Items[j]的操作来完成,但只能通过lv.Items[j]所属的类来完成可以转换的是我的SampleDataContent,但不是任何控件或UIFrameworkElement。我不会用代码隐藏来设置它。我宁愿用自定义转换器将要设置样式的属性绑定到模板项。我可以向XAML标记添加一些模板,但我如何选择分配给当前ListViewItem的控件来设置样式。谢谢。我不会这样做ry it.xum59,谢谢你的回答。WinRT表示法可能有一些不同,但我理解了风格转换的本质。现在我可以在正确的方向上使用google和msdn。谢谢!xum59的回答绝对正确。最终的XAML和代码如下: