나는 사용자 지정 스타일리스트 박스가 있습니다프로그래밍 방식으로 데이터 바인딩 ListBox 컨트롤에서 항목을 선택하는 방법
<phone:PhoneApplicationPage.Resources>
<Style x:Key="LayoutsListItemStyle" TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Grid Height="170" Width="170" Margin="0,0,20,20">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="LayoutStates">
<VisualState x:Name="BeforeUnloaded"/>
<VisualState x:Name="BeforeLoaded"/>
<VisualState x:Name="AfterLoaded"/>
</VisualStateGroup>
<VisualStateGroup x:Name="SelectionStates">
<VisualState x:Name="Unselected"/>
<VisualState x:Name="Selected">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderThickness)" Storyboard.TargetName="border">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<Thickness>4</Thickness>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="SelectedUnfocused"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Border x:Name="border" Width="170" Height="170" BorderBrush="{StaticResource PhoneAccentBrush}">
<Grid>
<Image Source="{Binding ThumbnailPath}" Width="170" Height="170" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</phone:PhoneApplicationPage.Resources>
수동으로 선택하면 선택한 목록 상자 항목 (위에 테두리를 표시
<ListBox x:Name="LayoutsList" ItemContainerStyle="{StaticResource LayoutsListItemStyle}" ItemsSource="{Binding}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<toolkit:WrapPanel Orientation="Horizontal" MaxWidth="410" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
). 목록 상자의 항목을 라디오 단추처럼 작동시키고 목록 상자의 첫 번째 항목을 기본적으로 선택하고 싶습니다.
는이 같은 목록 상자의 selectedIndex의 설정하려고 해요 :private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
// Loads a list of available Layouts to a ListBox
XDocument layoutSummary = XDocument.Load("Content/LayoutSummary.xml");
var layouts =
from elem in layoutSummary.Descendants("ComicLayout")
select new ComicLayout
{
Name = (string)elem.Attribute("Name").Value,
FriendlyName = (string)elem.Attribute("FriendlyName").Value,
ThumbnailPath = "Content/LayoutIcons/" + (string)elem.Attribute("Name").Value + ".png"
};
LayoutsList.DataContext = layouts;
LayoutsList.SelectedIndex = 1;
}
을하지만 아무것도하지 않는 것.
어떻게 프로그래밍 방식으로 데이터 바인딩 된 ListBox의 첫 번째 (또는 다른) 항목을 선택할 수 있습니까?
편집
이 selectedIndex의 실제로 작동하고 나는 그것을 제어하고 내가 원하는대로 목록 상자에서 데이터를 가져올 수 있음을 밝혀졌습니다.
그래서 나는 문제가 될 것 같아요는 어떻게 프로그래밍 방식으로 목록 상자 항목에 VisualState 변화를 트리거?
나는 어떻게 그럴 수 있습니까? 예를 올리시겠습니까? – LukeP
Nevermind, SelectedIndex 실제로 작동, 그냥 목록 항목의 visualstate 변경되지 않습니다. 나는 원래의 질문을 편집했다. – LukeP