Почему я не могу выбрать цвет фона выбранного ListBoxItem в WPF?

Safari ... Он не учитывает смещение часового пояса, когда создает экземпляр со строкой даты.

Добавление Z в конец также неплохо, но если вы хотите получить тот же результат с другими браузерами, следует рассчитать смещение часового пояса.

Вот что я сделал ...

// Before do this, check navigator.userAgent 
// and execute below logic if it is desktop Safari.

// Add Z is the convention, but you won't get any error even if do not add.
var c = new Date('2019-01-19 23:59:59Z'.replace(/\s+/g, 'T')) 

// It will returns in minute
var timeOffset = new Date().getTimezoneOffset();

// Do not forget getTime, if not, you will get Invalid date         
var d = new Date(c.getTime() + (timeOffset*60*1000))    

Открою этот пост до завтра в ожидании лучшего ответа. Спасибо.

5
задан Dave Clemmer 28 July 2011 в 19:59
поделиться

3 ответа

Это может быть сделано намного более простое. Цвет фона для выбранных объектов ListBox взят от SystemColors. Так, что необходимо сделать, переопределить SystemColors в Ресурсах ListBox:

<ListBox.Resources>
    <!--Selected color when the ListBox is focused-->
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Yellow" />
    <!--Selected color when the ListBox is not focused-->
    <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Yellow" />
</ListBox.Resources>
12
ответ дан 18 December 2019 в 09:54
поделиться

Этот код должен работать на установку фона. Проблема состоит в том, что необходимо создать ControlTemplate и присвоить свойству Background ContentPresenter "Желтое" значение.

    <Style x:Key="ListBoxItemStyle1" TargetType="{x:Type ListBoxItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListBoxItem}">
                    <Border x:Name="Bd" SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" x:Name="contentPresenter"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected" Value="true">
                            <Setter Property="OpacityMask" TargetName="contentPresenter" Value="{x:Null}"/>
                            <Setter Property="Background" TargetName="Bd" Value="Yellow"/>
                            <Setter Property="FontWeight" Value="Bold"/>
                            <Setter Property="FontSize" Value="18"/>
                            <Setter Property="Foreground" Value="Red"/>
                        </Trigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsSelected" Value="true"/>
                                <Condition Property="Selector.IsSelectionActive" Value="false"/>
                            </MultiTrigger.Conditions>
                            <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
                            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
                        </MultiTrigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
3
ответ дан 18 December 2019 в 09:54
поделиться

Спасибо, Фрэнсис! Это сделало это для меня, ну, в некоторой степени. Вот мой код, который позволяет шаблону использовать свойство «StrColor» для выбранных и невыбранных элементов списка.

    <Style TargetType="ListBoxItem">
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <!--Nice Brush-->
        <Setter Property="Background">
            <Setter.Value>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <!-- This is a gradient from white to StrColor and back to white -->
                    <!--<GradientStop Color="White" Offset="0"/>
                    <GradientStop Color="{Binding Path=StrColor}" Offset="0.445"/>
                    <GradientStop Color="White" Offset="1"/>
                    <GradientStop Color="{Binding Path=StrColor}" Offset="0.53"/>-->
                    <!-- This is a gradient from StrColor to white -->
                    <GradientStop Color="{Binding Path=StrColor}" Offset="0"/>
                    <GradientStop Color="White" Offset="1"/>
                </LinearGradientBrush>
            </Setter.Value>
        </Setter>
        <!--Standard Color-->
        <!--<Setter Property="Background" Value="{Binding Path=StrColor}"/>-->
        <Setter Property="Foreground" Value="{Binding Path=TextColor}"/>
        <Setter Property="Height" Value="{Binding Path=Height}"/>
        <Setter Property="Margin" Value="0"/>
        <Setter Property="Focusable" Value="False"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListBoxItem}">
                    <Border x:Name="Bd" SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" x:Name="contentPresenter"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected" Value="true">
                            <Setter Property="OpacityMask" TargetName="contentPresenter" Value="{x:Null}"/>
                            <Setter Property="Background" TargetName="Bd">
                                <Setter.Value>
                                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                        <GradientStop Color="{Binding Path=StrColor}" Offset="0"/>
                                        <GradientStop Color="White" Offset="1"/>
                                    </LinearGradientBrush>
                                </Setter.Value>
                            </Setter>
                            <Setter Property="Foreground" Value="{Binding Path=TextColor}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</UserControl.Resources>
0
ответ дан 18 December 2019 в 09:54
поделиться
Другие вопросы по тегам:

Похожие вопросы: