14

私の主な質問は、コントロール(データグリッド内のコンボボックス)に複数のソースがある(またはdatacontextとitemssourceの両方がある)シーンでのバインディングケースについてです。では、バインディングがどのソースを使用するかをどうやって知ることができますか?(それを明確にするための構文)

データグリッドにitemssource="List of Players" datacontext = "Manager"があり、一種の列としてコンボボックスがあると仮定します。また、各プレーヤーにはコレクションタイプであるInventoryプロパティがあると想定しています。

次に、datagrid.columns内:

  1. (バインディング用の)各列の現在のソースはPlayerです(これは私がこれまでに理解している方法です)。プレイヤーのプロパティにのみバインドでき、データコンテキスト「マネージャー」のプロパティにはバインドできません。「マネージャ」のプロパティにバインドする方法はありません。私は正しいですか?
  2. ただし、コンボボックスの列に移動すると、コンボボックスのitemssource ='player's Inventory'とすると、comboboxItemの現在のソースはインベントリ内の各アイテムになります。そして、バインディングを使用すると、それらのアイテムのプロパティにのみバインドできます。ただし、コンボボックスのプロパティ内のプレーヤーのプロパティ、特にSelectedValueとSelectedItemにバインドできるコードが表示されることがあります。私はここで少し混乱していますあなたは私を助けることができますか?

ありがとうございました

4

2 に答える 2

13

考慮すべき重要なコントロールはItemsControlComboBoxから継承し、非常によく似ItemsControlた動作をします)です。DataGrid

タイプのプロパティがありItemsControlます。また、プロパティがあります。のすべてのアイテムに対して、そのコピーを1つ作成します。のは、の各アイテムになります。ItemsSourceIEnumerableItemTemplateItemTemplateItemsSourceDataContextItemTemplateItemsSource

の場合ComboBox、's列のがオブジェクトになります。'sを'sインベントリにバインドすると、 'sリストの各アイテムが取得されます。 注意すべきことはそれ自体のは変更されていないということです。それはまだオブジェクトです。のを指定すると、それがのインベントリ内のアイテムになります。DataContextDataGridPlayerComboBoxItemSourcePlayerComboBox
DataContextComboBoxPlayerItemTemplateComboBoxDataContextPlayer

于 2010-08-19T20:17:28.497 に答える
9

その本当に簡単です。

DataContextは、アイテムの同じプロパティを参照します。それは拡張されず、動的ではありません。DataContextは、現在親の内部にある子のプロパティに適用されます。

ただし、ItemsSourceは動的です。ソースとともに拡張されます。これがgudの例です。

これはサンプルxamlです。

 <UserControl x:Class="SilverlightApplication"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">

<Grid x:Name="LayoutRoot" Background="White">
    <Grid.Resources>           
        <DataTemplate x:Key="template2">
            <StackPanel Orientation="Horizontal">
                <Image x:Name="img1" Source="{Binding Image}"></Image>
                <TextBlock x:Name="data2" Text="{Binding Data}"></TextBlock>
            </StackPanel>
        </DataTemplate>
    </Grid.Resources>
    <StackPanel>
        <StackPanel x:Name="DataContextStack" Orientation="Vertical">
            <TextBlock x:Name="data1" Text="{Binding Text1}"></TextBlock>
            <TextBlock x:Name="data2" Text="{Binding Text2}"></TextBlock>
        </StackPanel>
        <ListBox x:Name="lst2" ItemTemplate="{StaticResource template2}"></ListBox>
    </StackPanel>
</Grid>

これが背後にあるコードです。

 namespace SilverlightApplication
 {
  public partial class MainPage : UserControl
 {
    public MainPage()
    {
        InitializeComponent();
        loadLists();
    }

    private void loadLists()
    {
        ObservableCollection<Temp2> tColl = new ObservableCollection<Temp2>();            

        Temp1 t1 = new Temp1();
        t1.Text1 = "DataContext1";
        t1.Text2 = "DataContext2";            

        tColl.Add(new Temp2() { Image = "", Data = "Item1" });
        tColl.Add(new Temp2() { Image = "", Data = "Item2" });


        DataContextStack.DataContext = t1;
        lst2.ItemsSource = tColl;            
    }
}

public class Temp1
{
    public string Text1 { get; set; }
    public string Text2 { get; set; }



}

public class Temp2
{
    public string Image { get; set; }
    public string Data { get; set; }
}
}

ご覧のとおり、DataContextはStackPanelに存在するTextblockに適用され、Textである単一のプロパティを参照します。

ItemsSourceはTextblockのImageandTextプロパティのソースを参照し、リスト内のアイテムはObservableCollectionとともに拡張できます。

または、それをさらに簡単にするために。

DataContext-値は設計に基づいて設定されます。ItemsSource-値はロジックに基づいて設定されます。

お役に立てれば。

これがあなたの質問に答えた場合、これを答えとしてマークしてください。

于 2010-08-20T16:53:18.740 に答える