将背景颜色添加到Listview的特定行

时间:2014-06-08 09:59:56

标签: c# wpf

目标:
如果当前时间超过约会时间,请将列表视图的背景颜色更改为浅灰色。例如,时间是10.15,你有背景颜色09.30 - 10.30和10.00 - 11.00。

问题:
我不知道为了达到目标我需要使用什么语法。我也回顾了不同网站的一些建议,他们的建议还不够。

信息:
- 我正在使用Nhibernate来检索数据 - 这是一个用户控件。

enter image description here

   private void DisplayActivityBasedonSelectedDate()
        {
             DateTime aa = (DateTime) cdate_left.SelectedDate;

             lvw_aktivitet.DataContext = _myNhibernateDataProvider.RetrieveAllActivitetBasedOnDate(aa);

             DisplayDateInTheLabel();
        }


        public IList<Aktivitet_data> RetrieveAllActivitetBasedOnDate(DateTime pDatum)
        {
            return _session.GetNamedQuery("sp_retrieveAllActivitetBasedOnDate").SetDateTime("Datum", pDatum)
                        .SetResultTransformer(Transformers.AliasToBean(typeof(Aktivitet_data))).List<Aktivitet_data>();

        }



    public class Aktivitet_data
    {
        public string PK_Aktivitet_schema { get; set; }
        public string Datum { get; set; }
        public string Tid { get; set; }
        public string Aktivitet { get; set; }
        public string Total_platser { get; set; }
        public string Bokade { get; set; }
        public string Drop_in { get; set; }
    }

XAML:

<UserControl x:Class="usercontrol_bokning.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid Background="White" Margin="-127,0,-777,-316">
        <ListView x:Name="lvw_aktivitet" HorizontalAlignment="Left" ItemsSource="{Binding}" SelectionMode="Single" Height="337" Margin="217,147,0,0" VerticalAlignment="Top" Width="365" SelectionChanged="lvw_aktivitet_SelectionChanged">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Tid" Width="auto" DisplayMemberBinding="{Binding Path=Tid}" TextBlock.TextAlignment="Left" />
                    <GridViewColumn Header="Aktivitet" Width="auto" DisplayMemberBinding="{Binding Path=Aktivitet}" TextBlock.TextAlignment="Left"  />
                    <GridViewColumn Header="Total platser" Width="auto" DisplayMemberBinding="{Binding Path=Total_platser}" TextBlock.TextAlignment="Center"  />
                    <GridViewColumn Header="Bokade" Width="auto" DisplayMemberBinding="{Binding Path=Bokade}" TextBlock.TextAlignment="Center"  />
                    <GridViewColumn Header="Drop in" Width="auto"  >
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock TextAlignment="Right" Block.TextAlignment="Right" Text="{Binding Path=Drop_in}"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>
        <Calendar x:Name="cdate_left" HorizontalAlignment="Left" Margin="10,147,0,0" VerticalAlignment="Top" SelectedDatesChanged="cdate_left_SelectedDatesChanged"/>
        <Button x:Name="btn_idag" Content="Idag" HorizontalAlignment="Left" Margin="217,122,0,0" VerticalAlignment="Top" Width="75" Click="btn_idag_Click"/>
        <Button x:Name="arrow__left" Click="Arrow_left_Click" Background="Transparent">
            <Button.Template>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Image x:Name="bild_arrow_left" HorizontalAlignment="Left" Height="22" Margin="302,121,0,0" VerticalAlignment="Top" Width="22" Source="C:\Users\Administrator\Downloads\left.jpg" />
                </ControlTemplate>
            </Button.Template>
        </Button>
        <Button x:Name="arrow__right" Click="Arrow_right_Click" Background="Transparent" >
            <Button.Template>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Image x:Name="bild_arrow_right" HorizontalAlignment="Left" Height="22" Margin="326,121,0,0" VerticalAlignment="Top" Width="22" Source="C:\Users\Administrator\Downloads\right.jpg"/>
                </ControlTemplate>
            </Button.Template>
        </Button>
        <Label x:Name="lbl_DisplayDateBySelection" Content="" HorizontalAlignment="Left" Margin="396,119,0,0" VerticalAlignment="Top"/>        
        <TabControl HorizontalAlignment="Left" Height="420" Margin="603,75,0,0" VerticalAlignment="Top" Width="589">
            <TabItem Header="Bokade">
                <Grid Background="#FFE5E5E5">
                    <ListView x:Name="lvw_bokade" HorizontalAlignment="Left" Height="335" Margin="10,46,0,0" VerticalAlignment="Top" Width="561">
                        <ListView.View>
                            <GridView>
                                <GridViewColumn/>
                            </GridView>
                        </ListView.View>
                    </ListView>
                    <Button Content="Ny plats" HorizontalAlignment="Left" Margin="10,21,0,0" VerticalAlignment="Top" Width="75"/>
                </Grid>
            </TabItem>
            <TabItem Header="Avbokade">
                <Grid Background="#FFE5E5E5" Margin="0,1,0,-1"/>
            </TabItem>
            <TabItem Header="Avregistrerade" HorizontalAlignment="Left" Height="20" VerticalAlignment="Top" Width="55">
                <Grid Background="#FFE5E5E5"/>
            </TabItem>
        </TabControl>
        <Label x:Name="lbl_instruktor" Content="Instruktör: " HorizontalAlignment="Left" Margin="1009,122,0,0" VerticalAlignment="Top"/>


    </Grid>
</UserControl>

1 个答案:

答案 0 :(得分:0)

如果您希望它自动更新项目,您将不得不使用某种计时器。 我在DispatcherTimer

中使用UserControl全局设置
public DispatcherTimer dispatcher;

On Loaded / Ctor

dispatcher = new DispatcherTimer();
dispatcher.Tick += CheckTime;
dispatcher.Interval = new TimeSpan(0, 0, 5); //Change the interval to whatever suits you best
dispatcher.Start();

以及检查数据的方法:

private void CheckTime(object sender, EventArgs e)
{
    ListBoxItem lbitem;
    foreach (var item in lvw_aktivitet.Items)
    {
        if ((item as Aktivitet_data).Tid > DateTime.Now)
        {
            lbitem = (ListBoxItem)lvw_aktivitet.ItemContainerGenerator.ContainerFromItem(item);
            lbitem.Background = new SolidColorBrush(Colors.Gray);   
        }
    }
}