简体   繁体   English

WinRT Xaml为ListViewItems设置不同的样式

[英]WinRT Xaml Setting different styles to ListViewItems

I have Outlook-liked application. 我有类似Outlook的应用程序。 There are 3 Sections. 有3个部分。 Middle Section contains ListView. 中间部分包含ListView。 Elements of Middle Section ListView have calculated styles 中段ListView的元素具有计算出的样式

(Red Font = Outdated Jobs, Bolt Font = Unread Jobs, Strikeout text = Performed Jobs) (红色字体=过时的作业,螺栓字体=未读的作业,删除线文本=执行的作业)

. Conditions may intersect in a different variations. 条件可能会以不同的变化相交。

I have Xaml markup of Section2: 我有Section2的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>

And I need to set up different styles of ListViewItem by the different conditions. 而且我需要根据不同的条件设置不同的ListViewItem样式。

There is code behind it: 后面有代码:

     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;      

How can I set up ListViewItem style 如何设置ListViewItem样式

I'm no WinRT expert but this may help : 我不是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();
        }
    }

Then, your ListView may be something like : 然后,您的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>

Any flag checked in the SampleTaskItem will trigger the desired style, they can combine from none to all. SampleTaskItem选中的任何标志都会触发所需的样式,它们可以从无到有结合在一起。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM