1

私はWPFWindowsアプリケーションに取り組んでいます。ItemsControlを使用してコレクションリストを表示しています。これに取り組んでいると、ItemsControlにSelectedItemプロパティがないことがわかりました。次に、ItemsControlから選択したアイテムを取得するにはどうすればよいですか。また、ItemsControlのヘッダーを表示するにはどうすればよいですか。

<ItemsControl ItemsSource="{Binding CustomSalesProducts, Mode=TwoWay}">
        <ItemsControl.Template>
            <ControlTemplate TargetType="ItemsControl">
                <Border>
                    <ScrollViewer VerticalScrollBarVisibility="Auto">
                        <ItemsPresenter/>
                    </ScrollViewer>
                </Border>
            </ControlTemplate>
        </ItemsControl.Template>
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <VirtualizingStackPanel CanHorizontallyScroll="True" CanVerticallyScroll="True" Orientation="Vertical"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Grid x:Name="SalesGrid" Background="White">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>
                    <controls:HeaderedContentControl Header="{Binding ProductName, Mode=TwoWay}" Margin="{DynamicResource Margin4}" Style="{DynamicResource HeaderedContentControlStyle}" HorizontalContentAlignment="Right">
                    </controls:HeaderedContentControl>
                    <TextBox Text="{Binding OrderQty, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Row="1" Margin="{StaticResource Margin4}" Style="{DynamicResource MiniTextBoxStyle}" ToolTip="Quantity" />
                    <TextBlock Text="{Binding UnitSalePrice, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Column="1" Grid.Row="1" Margin="{StaticResource Margin4}" ToolTip="Price"/>
                    <TextBox Text="{Binding Discount, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Column="2" Grid.Row="1" Margin="{StaticResource Margin4}" Style="{DynamicResource MiniTextBoxStyle}" ToolTip="Discount"/>
                    <TextBlock Text="{Binding TaxAmount, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Column="3" Grid.Row="1" Margin="{StaticResource Margin4}" ToolTip="Tax Amount"/>
                    <TextBlock Text="{Binding LineTotal, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Column="4" Grid.Row="1" Margin="{StaticResource Margin4}" ToolTip="Total"/>

                </Grid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

ありがとう、

4

3 に答える 3

1

あなたが言ったように、にはありませSelectedItemItemsControl。代わりに使用できますListBox

于 2012-06-08T08:13:30.427 に答える
0

ヘッダーを使用するためにHeaderdItemsControlがあることがわかりました。これでヘッダーを追加できますが、繰り返しはできません。ただし、これに関する問題は、自動サイズを定義する場合、ヘッダーとそのアイテムの静的サイズを定義する必要があることです。headeredItemsControlのUIは完全ではないため、静的サイズを指定する必要があります。

あなたはHeaderedItemsControlを使用する方法についてこれを読むことができますか?

于 2012-06-13T03:40:48.583 に答える
0

パーティーに少し遅れましたが、同じ問題に遭遇しました。これが他の誰かに役立つことを期待して、リストボックスを使用したくなかったので、自分のSelectedItemをロールした方法を説明します。

SelectedCustomSalesProductプロパティをDataContextとして使用しているクラスに公開し、アイテムが選択されたときに設定することで、選択したアイテムを自分で追跡できます。

SalesGridで、MouseLeftButtonDownおよびTouchDownイベントのイベントハンドラーを追加し、Tagプロパティを使用して、そのようにレンダリングされているアイテムへの参照を保持できます。

私の場合、グリッドの代わりにStackPanelを使用しており、以下のコードをコンパイルしなかったことに注意してください。説明のために使用してください。

この例を使用すると、一般的なアイデアを取得し、ビジネスサービスで選択したアイテムを設定できるはずです。

<DataTemplate>
    <Grid x:Name="SalesGrid" Background="White"
          Tag="{Binding}" 
          TouchDown="DataTemplate_Touch"
          MouseLeftButtonDown="DataTemplate_Click">

次に、背後にあるUserControl / windowのコードで、選択したアイテムを次のように追跡できます。

/// <summary>
/// MyScreen.xaml
/// </summary>
public partial class MyScreen : UserControl
{
    private MyServiceWrapper _serviceWrapper;

    public MyScreen()
    {
        InitializeComponent();
    }
    public MyScreen(MyServiceWrapper serviceWrapper)
    {
        //Instrumentation.Log(typeof(MyScreen), LogTypes.Trace, "Creating instance of MyScreen");

        this._serviceWrapper = serviceWrapper;

        // Set your DataContext, is this the class that would also have your 
        // CustomSalesProducts property exposed 
        this.DataContext = this._serviceWrapper;
        InitializeComponent();
    }



    private void DataTemplate_Touch(object sender, System.Windows.Input.TouchEventArgs e)
    {
        SetSelectedCustomSalesProduct(sender);
    }

    private void DataTemplate_Click(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        SetSelectedCustomSalesProduct(sender);
    }

    private void SetSelectedCustomSalesProduct(object sender)
    {
        _serviceWrapper.SelectedCustomSalesProduct = ((Grid)sender).Tag as CustomSalesProduct;

    }



}
于 2014-09-02T19:21:05.633 に答える