1

I have a ListBox:

<ListBox x:Name="Menu" Grid.Row="1" ScrollViewer.HorizontalScrollMode="Disabled" ScrollViewer.VerticalScrollMode="Disabled" ItemContainerStyle="{StaticResource ListBoxItemStyle1}" ItemTemplate="{StaticResource DataTemplate1}">

</ListBox>

Here is my Data Template:

<DataTemplate x:Key="DataTemplate1">
        <Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="13"/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <TextBlock x:Name="IconPresenter" Grid.Column="0" Text="{Binding Icon}" />
            <TextBlock x:Name="TextPresenter" Grid.Column="1" Text="{Binding Name}" />
        </Grid>
</DataTemplate>

And here is my ListBoxItemStyle ( i havent made any modifications to it, except some colors, so i will not paste all the XAML ):

<Style x:Key="ListBoxItemStyle1" TargetType="ListBoxItem">
    <Setter Property="IsEnabled" Value="{Binding DoesNotNeedLogin}"/>
    ...
</Style>

I'm trying to set Menu.ItemsSource so i could also specify if this item will be enabled or not.

For example:

public class Test
{
    public string Name { get; set; }
    public bool DoesNotNeedLogin { get; set; }
}

List<Test> list = new List<Test>() { new Test() { Name = "One", DoesNotNeedLogin = true }, new Test() { Name = "One" }, new Test() { Name = "One" }, new Test() { Name = "two", DoesNotNeedLogin = true } };
Menu.ItemsSource = list;

For those elements that have DoesNotNeedLogin = true i want ListBoxItems to be enabled, for those that have false - disabled. What am i doing wrong?

Here is my full XAML code:

<UserControl
    x:Class="Project.Controllers.MenuController"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Project.Controllers"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="700"
    d:DesignWidth="300">
    <UserControl.Resources>
        <Style x:Key="ListBoxStyle1" TargetType="ListBox">
            <Setter Property="Foreground" Value="{StaticResource MenuItemForeground}"/>
            <Setter Property="Background" Value="{StaticResource MenuItemBackground}"/>
            <Setter Property="BorderBrush" Value="Transparent"/>
            <Setter Property="BorderThickness" Value="0"/>
            <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled"/>
            <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
            <Setter Property="ScrollViewer.HorizontalScrollMode" Value="Disabled"/>
            <Setter Property="ScrollViewer.VerticalScrollMode" Value="Auto"/>
            <Setter Property="ScrollViewer.ZoomMode" Value="Disabled"/>
            <Setter Property="ScrollViewer.IsDeferredScrollingEnabled" Value="False"/>
            <Setter Property="ScrollViewer.BringIntoViewOnFocusChange" Value="True"/>
            <Setter Property="IsTabStop" Value="False"/>
            <Setter Property="TabNavigation" Value="Once"/>
            <Setter Property="FontFamily" Value="{StaticResource ApplicationFont}"/>
            <Setter Property="FontSize" Value="{StaticResource ApplicationCommonFontSize}"/>
            <Setter Property="ItemsPanel">
                <Setter.Value>
                    <ItemsPanelTemplate>
                        <VirtualizingStackPanel/>
                    </ItemsPanelTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBox">
                        <Border x:Name="LayoutRoot" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}">
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualState x:Name="Normal"/>
                                    <VisualState x:Name="Disabled">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="LayoutRoot">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="LayoutRoot">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ListBoxDisabledForegroundThemeBrush}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                            <ScrollViewer x:Name="ScrollViewer" AutomationProperties.AccessibilityView="Raw" BringIntoViewOnFocusChange="{TemplateBinding ScrollViewer.BringIntoViewOnFocusChange}" HorizontalScrollMode="{TemplateBinding ScrollViewer.HorizontalScrollMode}" HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}" IsHorizontalRailEnabled="{TemplateBinding ScrollViewer.IsHorizontalRailEnabled}" IsVerticalRailEnabled="{TemplateBinding ScrollViewer.IsVerticalRailEnabled}" IsDeferredScrollingEnabled="{TemplateBinding ScrollViewer.IsDeferredScrollingEnabled}" Padding="{TemplateBinding Padding}" TabNavigation="{TemplateBinding TabNavigation}" VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}" VerticalScrollMode="{TemplateBinding ScrollViewer.VerticalScrollMode}" ZoomMode="{TemplateBinding ScrollViewer.ZoomMode}">
                                <ItemsPresenter/>
                            </ScrollViewer>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <Style x:Key="ListBoxItemStyle1" TargetType="ListBoxItem">
            <!--<Setter Property="IsEnabled" Value="{Binding DoesNotNeedLogin}"/>-->
            <Setter Property="IsEnabled" 
                Value="{Binding RelativeSource={RelativeSource TemplatedParent}, 
                Path=DoesNotNeedLogin}"/>
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="TabNavigation" Value="Local"/>
            <Setter Property="Padding" Value="10,0,0,0"/>
            <Setter Property="HorizontalContentAlignment" Value="Left"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBoxItem">
                        <Border x:Name="LayoutRoot" Height="{StaticResource MenuItemHeight}" BorderBrush="{StaticResource MenuItemShadowColor}" BorderThickness="0,0,0,1" >
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualState x:Name="Normal"/>
                                    <VisualState x:Name="PointerOver"/>
                                    <VisualState x:Name="Disabled">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="LayoutRoot">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource MenuItemDisabledBackground}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource MenuItemDisabledForeground}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Pressed">
                                        <Storyboard>
                                            <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="PressedBackground"/>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource MenuItemPressedForeground}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                                <VisualStateGroup x:Name="SelectionStates">
                                    <VisualState x:Name="Unselected"/>
                                    <VisualState x:Name="Selected">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="InnerGrid">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource MenuItemActiveBackground}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource MenuItemActiveForeground}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="SelectedUnfocused">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="InnerGrid">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource MenuItemActiveBackground}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource MenuItemActiveForeground}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="SelectedDisabled">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="InnerGrid">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource MenuItemDisabledBackground}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource MenuItemDisabledForeground}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="SelectedPointerOver">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="InnerGrid">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource MenuItemPressedBackground}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource MenuItemPressedForeground}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="SelectedPressed">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="InnerGrid">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource MenuItemPressedBackground}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource MenuItemPressedForeground}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                            <Border BorderBrush="#00457C" BorderThickness="0,0,0,1" Background="{TemplateBinding Background}">
                                <Grid x:Name="InnerGrid" Background="Transparent">
                                    <Rectangle x:Name="PressedBackground" Fill="{StaticResource MenuItemPressedBackground}" Opacity="0"/>
                                    <ContentPresenter x:Name="ContentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                                </Grid>
                            </Border>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <DataTemplate x:Key="DataTemplate1">
            <Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="13"/>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>
                <TextBlock x:Name="IconPresenter" Grid.Column="0" Text="{Binding Icon}" />
                <TextBlock x:Name="TextPresenter" Grid.Column="1" Text="{Binding Name}" />
            </Grid>
        </DataTemplate>
    </UserControl.Resources>

    <Grid Background="{Binding Mode=OneWay, Source={StaticResource MenuBackground}}">
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="auto"/>
        </Grid.RowDefinitions>
        <!-- Avatar and greeting -->
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="auto"/>
                <RowDefinition Height="auto"/>
            </Grid.RowDefinitions>
            <!-- User avatar -->
            <Image Grid.Row="0" Margin="0,21,0,7" Height="120" Source="ms-appx:///Assets/Icons/no-session-profile-icon.png"/>
            <!-- User greeting text -->
            <TextBlock Grid.Row="1" Margin="0,0,0,15" FontSize="15"  Text="{Binding LocalizedResources, Converter={StaticResource LocalizationConverter}, Mode=OneWay, ConverterParameter=MenuWelcome_NotLoggedIn, Source={StaticResource LocalizationHelper}}" HorizontalAlignment="Center" FontFamily="{Binding Source={StaticResource ApplicationFont}}"/>
        </Grid>
        <!-- Menu items ( pages ) -->
        <ListBox x:Name="Menu" Grid.Row="1"  >
            <ListBox.ItemContainerStyle>
                <Style TargetType="ListBoxItem">
                    <Setter Property="IsEnabled" Value="{Binding IsEnabled}"/>
                </Style>
            </ListBox.ItemContainerStyle>
        </ListBox>
    </Grid>
</UserControl>
Developer
  • 4,158
  • 5
  • 34
  • 66
  • The answer is ItemContainerStyle. If that isn't working for you, update your question with an [edit] that details how you are using it (no evidence of such in your current question) and I'll reopen. –  Aug 30 '16 at 13:01
  • @Will And what should i edit if i have seen all the answers! But they do not work in Windows Phone 8.1 – Developer Aug 30 '16 at 13:54
  • Odd, ItemContainerStyle should exist on wp8.1 and it should work. Where are you defining the Style element in your xaml? –  Aug 30 '16 at 14:12
  • @Will In the same file as DataContainer and ListBox. In . I mean that ListBox is in UserControl and Styles are inside – Developer Aug 30 '16 at 14:21
  • Please don't add relevant info in comments. [edit] your question to show how you are defining it. Did you try to use ListBox.ItemContainerStyle? –  Aug 30 '16 at 14:27
  • @Will Yes, same result – Developer Aug 30 '16 at 14:35
  • Not sure why that would be. But glad you've clarified that, it will be of use in getting your answer. Good luck. –  Aug 30 '16 at 14:36

0 Answers0