4

WPFでは、ラジオボタンをこのようなViewModelのプロパティにバインドしようとしています。SO回答https://stackoverflow.com/a/2285732

ボタンが垂直に積み重ねられていることを除いて、すべてが正常に機能します。さて、これは簡単な修正のようです。ItemsPanelTemplateを変更するだけです。

これが私のコードです:

<ListBox ItemsSource="{Binding ItemOptions}" SelectedItem="{Binding SelectedOption}">
     <ListBox.ItemsPanel>
          <ItemsPanelTemplate>
               <StackPanel Orientation="Horizontal" IsItemsHost="True" />
          </ItemsPanelTemplate>
     </ListBox.ItemsPanel>
     <ListBox.ItemContainerStyle>
          <Style TargetType="{x:Type ListBoxItem}">
               <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ListBoxItem}"  >
                            <RadioButton Content="{TemplateBinding Content}" 
                                 IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsSelected}"/>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
           </Style>
     </ListBox.ItemContainerStyle>
</ListBox>

ただし、アイテムは垂直に積み重ねられたままです。これがリストボックスの向きに影響を与えない理由はありますか?

4

2 に答える 2

4

これを試して:

<ListBox.Template>
      <ControlTemplate TargetType="{x:Type ListBox}">
          <ScrollViewer x:Name="scrollviewer" 
                        HorizontalScrollBarVisibility="Visible" CanContentScroll="False">
            <StackPanel IsItemsHost="True" Orientation="Horizontal" />
          </ScrollViewer>
      </ControlTemplate>
 </ListBox.Template>

私はこれをItemsPanelTemplateで機能させようとしましたが、成功しませんでした。これは私にとってはうまくいきました。よろしく

于 2013-01-18T19:11:42.403 に答える
0

これは、スタイリングなしの基本的な例です。WrapPanelはレイアウトを処理することに注意してください。

    <ListBox Margin="0,10,0,0"        
             ItemsSource="{StaticResource Orders}">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel HorizontalAlignment="Stretch"/>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <RadioButton Content="{Binding CustomerName}" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

コードビハインドで定義されたモデルのデータ。

<Page.Resources>
    <model:Orders x:Key="Orders">
        <model:Order CustomerName="Alpha"
                    OrderId="997"
                    InProgress="True" />
        <model:Order CustomerName="Beta"
                    OrderId="998"
                    InProgress="False" />
        <model:Order CustomerName="Omega"
                    OrderId="999"
                    InProgress="True" />
        <model:Order CustomerName="Zeta"
                    OrderId="1000"
                    InProgress="False" />
    </model:Orders>
</Page.Resources>

結果

ここに画像の説明を入力してください

于 2019-11-07T16:12:33.940 に答える