2012-09-04 2 views
14

WinRT 페이지 (XAML)의 "ListBox"에서 배경색을 변경하려고합니다. "Background"속성을 사용하면 컨트롤에 포커스가 없을 때 배경을 어떻게 바꿀 수 있습니다. 초점이 맞춰지면 흰색으로 바뀌고이를 무시하는 방법을 알 수 없습니다.ListBox 배경색 (XAML/WinRT/Metro)

내 질문, 어떻게 내가 ListBox의 배경을 강제로 회색으로 선택했는지 여부/포커스가 있는지 여부.

XAML # 1 :

<ListBox x:Name="ListBoxMenu" Background="LightGray" Grid.Row="0" Grid.Column="0" Margin="0,0,0,0"> 
     <ListBoxItem>Menu Item 1</ListBoxItem> 
     <ListBoxItem>Menu Item 2</ListBoxItem> 
     <ListBoxItem>Menu Item 3</ListBoxItem> 
    </ListBox> 

XAML # 2 (각 항목은 또한 집합)

<ListBox x:Name="ListBoxMenu" Background="LightGray" Grid.Row="0" Grid.Column="0" Height="124" VerticalAlignment="Top"> 
     <ListBoxItem Background="LightGray">Menu Item 1</ListBoxItem> 
     <ListBoxItem Background="LightGray">Menu Item 2</ListBoxItem> 
     <ListBoxItem Background="LightGray">Menu Item 3</ListBoxItem> 
    </ListBox> 

ListBox with Gray background when it doesn't have the focus

ListBox, resetting the background to white when it gets focus

임시 용액, I 세트 오직 하드 코딩 된 h 일 ListBox 그 다음에 LightGray를 사용하여 나머지 칸을 채우기 위해 그 칸에 테두리를 사용했습니다. 정말 ListBox에 배경색을 설정하고 싶습니다. 가능합니까?

+0

해결 방법에 대한 코드 스 니펫을 제공해주십시오. 나는 또한 동일한 문제를 가지고 있지만 수정할 수 없었다. – SachiraChin

+0

환경 설정에 따라 배경 변경을 트리거하는 하나 또는 두 개의 이벤트가있는 경우 ListBoxMenu.Background = Colors.Transparent를 이벤트 핸들러에 추가하기 만하면됩니다. – Hong

답변

5

Visual Studio Blend 2012를 사용하여 ListBox ItemTemplate 또는 해당 서식 파일을 편집하면 XAML에서 하드 카피가 만들어져 속성을 편집 할 수 있습니다.

+0

고마워요. 한번 시도해 보죠. –

+0

비주얼 블렌더 란 무엇입니까? 링크를 제공 할 수 있습니까? –

+1

그는 Visual Studio 용 블렌드를 의미한다고 생각합니다. ListBox를 마우스 오른쪽 버튼으로 클릭하고 스타일 편집을 사용하여 편집 할 수있는 하드 카피를 만들 수있었습니다. 내가 제공 한 템플릿을 변경할 수 있었기 때문에 Blend를 생략했습니다. –

3

동일한 문제가 발생하여 Visual Studio Blend의 도움을 받았습니다. 희망이 도움이됩니다. 다음과 같이

는 ListBoxMenu에 스타일을 추가 : 블랙에 상자 컨테이너를 나열의

<Style x:Key="ListBoxStyle1" TargetType="ListBox"> 
     <Setter Property="Foreground" Value="{StaticResource ListBoxForegroundThemeBrush}"/> 
     <Setter Property="Background" Value="{StaticResource ListBoxBackgroundThemeBrush}"/> 
     <Setter Property="BorderBrush" Value="{StaticResource ListBoxBorderThemeBrush}"/> 
     <Setter Property="BorderThickness" Value="{StaticResource ListBoxBorderThemeThickness}"/> 
     <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled"/> 
     <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/> 
     <Setter Property="ScrollViewer.HorizontalScrollMode" Value="Disabled"/> 
     <Setter Property="ScrollViewer.IsHorizontalRailEnabled" Value="True"/> 
     <Setter Property="ScrollViewer.VerticalScrollMode" Value="Enabled"/> 
     <Setter Property="ScrollViewer.IsVerticalRailEnabled" Value="True"/> 
     <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 ContentControlThemeFontFamily}"/> 
     <Setter Property="FontSize" Value="{StaticResource ControlContentThemeFontSize}"/> 
     <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="{StaticResource AppBarBackgroundThemeBrush}"> 
         <VisualStateManager.VisualStateGroups> 
          <VisualStateGroup x:Name="CommonStates"> 
           <VisualState x:Name="Normal"/> 
           <VisualState x:Name="Disabled"> 
            <Storyboard> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="LayoutRoot"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ListBoxDisabledForegroundThemeBrush}"/> 
             </ObjectAnimationUsingKeyFrames> 
            </Storyboard> 
           </VisualState> 
          </VisualStateGroup> 
          <VisualStateGroup x:Name="FocusStates"> 
           <VisualState x:Name="Focused"> 
           <Storyboard> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ScrollViewer"> 
             <DiscreteObjectKeyFrame KeyTime="0" Value="Black"/> 
            </ObjectAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
           <VisualState x:Name="Unfocused"/> 
          </VisualStateGroup> 
         </VisualStateManager.VisualStateGroups> 
         <ScrollViewer x:Name="ScrollViewer"> 
         <ItemsPresenter/> 
        </ScrollViewer> 
        </Border> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

위의 샘플 배경을 대체 할 다음과 같이

<ListBox x:Name="ListBoxMenu" Style="{StaticResource ListBoxStyle1} Background="LightGray" Grid.Row="0" Grid.Column="0" Height="124" VerticalAlignment="Top"> <ListBoxItem Background="LightGray">Menu Item 1</ListBoxItem> <ListBoxItem Background="LightGray">Menu Item 2</ListBoxItem> <ListBoxItem Background="LightGray">Menu Item 3</ListBoxItem> </ListBox>

가 그런 스타일을 지정 포커스가 ListBox로 설정된 경우. 이 같은 원리에 ListBox, ListView 또는 GridView, 그들은 모든 일에 Items의 색상을 사용자 정의에 좀 더 도움이 필요하면

2

, 그냥 비토 한 번 봐 가지고 권 해드립니다의 TargetType 속성을 업데이트해야합니다 DeMercurio의 블로그 게시물 Styling a GridViewItem in WinRT

6

XAML 리소스 파일에 일부 색 브러시 재정의를 적용하여 기본 ListBox 컨트롤 템플릿 색을 재정의 할 수 있습니다.

<SolidColorBrush x:Key="ListBoxBackgroundThemeBrush" Color="Transparent" /> 
<SolidColorBrush x:Key="ListBoxFocusBackgroundThemeBrush" Color="Transparent" /> 
+0

모든 ListBox에 대해 동일한 색상을 원한다면 멋진 솔루션입니다. –

+1

Windows 8에서는 더 이상 작동하지 않는다고 생각합니다.승 7에서만. – user3595338