문제

내가 만든 스타일의 ListBox 에서 WPF 그래서 그것으로 렌더링됩 체크 박스 목록입니다.

할 때 내가 채우는 ListBox 의 항목,수동으로 스타일을 완벽하게 작동합니다.그러나 나 대신에 바인딩 ItemsSource 의 ListBox 정적 리소스(인 ItemsControl 포함하는 데 필요한 항목),스타일이 완전히 삭제됩니다.

여기에서의 스타일:

<Style x:Key="CheckBoxListStyle" TargetType="ListBox">
    <Style.Resources>
        <Style TargetType="ListBoxItem">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBoxItem">
                        <Grid Margin="2">
                            <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition />
                            </Grid.ColumnDefinitions>
                            <CheckBox IsChecked="{Binding IsSelected, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"/>
                            <ContentPresenter
                                Grid.Column="1"
                                Margin="2,0,0,0" />
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Style.Resources>
    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <WrapPanel Orientation="Vertical"  />
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="BorderThickness" Value="0" />
    <Setter Property="Background" Value="Transparent" />
</Style>

여기에는 코드 대한 목록 상자에 표시하는 스타일을 올바르게:

<ListBox x:Name="ColumnsList"
            Grid.Column="0"
            Grid.Row="0"
            Style="{StaticResource CheckBoxListStyle}"
            BorderThickness="1">                                                
            <ListBox.Items>
                <ListBoxItem>Test</ListBoxItem>
                <ListBoxItem>Test2</ListBoxItem>
                <ListBoxItem>Test3</ListBoxItem>
            </ListBox.Items>
        </ListBox>

여기에는 코드 대한 목록 상자를 무시하는 스타일:

<ListBox x:Name="ColumnsList2"
            Grid.Column="0"
            Grid.Row="0"
            Style="{StaticResource CheckBoxListStyle}"
            BorderThickness="1"
            ItemsSource="{Binding Source={StaticResource Test1}, Path=Items}">
        </ListBox>

바라고 누군가가 도움이 될 수 있습니다-나는 모두에게 새로운 이 시도 모든 것을 생각할 수 있지만,모든 것을 내가 읽은 나를 믿는 설정 ItemsSource 가 동일한 결과로 설정 항목이동,그래서 나는 볼 수 없습니다 왜 이유를 이 작동하지 않을 것입니다.

감사합니다,

도움이 되었습니까?

해결책

스타일을 변경.자원하는 설정 ItemContainerStyle 시설 및 작동해야 다음과 같습니다.

    <Style x:Key="CheckBoxListStyle" TargetType="ListBox">
    <Setter Property="ItemContainerStyle">
        <Setter.Value>
            <Style TargetType="ListBoxItem">
                <Setter Property="Template">
                    <Setter.Value>
                            <ControlTemplate TargetType="ListBoxItem">
                            <Grid Margin="2">
                                <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto" />
                                        <ColumnDefinition />
                                </Grid.ColumnDefinitions>
                                <CheckBox IsChecked="{Binding IsSelected, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"/>
                                <ContentPresenter
                                    Grid.Column="1"
                                    Margin="2,0,0,0" />
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Setter.Value>
    </Setter>
    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <WrapPanel Orientation="Vertical"  />
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="BorderThickness" Value="0" />
    <Setter Property="Background" Value="Transparent" />
</Style>

이전 버전에서(전 SP1),를 정의할 때 Styles 에서 스타일,그 중 하나는 스타일은 무시됩니다.또는,당신은 설정할 수 있는 리소스의 스타일에 부모 자원..

희망이 도움이 됩니다!

다른 팁

이 때문에 당신의 된 targettype 에 CheckListBoxStyle 가를 대상으로 하는 ListBoxItem,하지만 당신이 설정 ItemSource 시설의 ListBox 당신이에 바인딩하는 목록의 다른 요소(수 예).즉 당신의 대상을 입력해야 int 대신 ListBoxItem.

또는 대상이 지정되지 않은 형식입니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top