How to link datagrid Silverlight itemSource shortcut list item to view Model

We use Caliburn.Micro / Silverlight 4, and life is good.

I am trying to link combobox itemsSource elements with viewModel, but this is not possible because combobox is already bound to its own dataItem line. The logic that fills the combo changes with other data on the screen, so I cannot use the static list as I used.

Is there any way to bind a directory to viewModel somehow ??? I tried to snap an element to an element binding, but this never works in a grid.

       <Controls:DataGridTemplateColumn x:Name="FooNameCol" Header="Foo" MinWidth="200">
            <Controls:DataGridTemplateColumn.CellTemplate>
                <DataTemplate>

                    <StackPanel>
                        <TextBlock Text="{Binding Path=Foo.ShortName}" 
                                   Style="{StaticResource DataGridTextColumnStyle}"/>
                    </StackPanel>

                </DataTemplate>
            </Controls:DataGridTemplateColumn.CellTemplate>
            <Controls:DataGridTemplateColumn.CellEditingTemplate>
                <DataTemplate>

                    <ComboBox DisplayMemberPath="ShortName"   
                              MinWidth="200" MinHeight="25"
                              SelectedItem="{Binding Path=Officer, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}"
                              ItemsSource="{Binding Officers, Source={StaticResource ReferenceListRetriever}}" />

                </DataTemplate>
            </Controls:DataGridTemplateColumn.CellEditingTemplate>
        </Controls:DataGridTemplateColumn>
+3
source share
1 answer

DataTemplate DataContext ; Bindings DataContext, , , , .

, :

public class MyVM {
   public IEnumerable<MyItem> Items {get;}
}

public class MyItem {
  public Foo Foo {get;}
  public Officer Officer {get;set;}
  public IEnumerable<Officer> Officers {get;}
}

, -. MyItem , MyVM:

public class MyItem {
  ...
  public IEnumerable<Officer> Officers {
    get { return _parent.AvailableOfficers; }
  }
}

, Xaml:

public class MyVM {
  public IEnumerable<MyItem> Items {get;}
  public IEnumerable<Officer> Officers {get;}
}

public class MyItem {
  public Foo Foo {get;}
  public Officer Officer {get;set;}
}

Xaml:

<UserControl ...>
  ...
  <AnyFrameworkElementAtThisLevel Name="bridge" />
  ...
  <Controls:WhateverGrid>
     ...
     <Controls:DataGridTemplateColumn ...>
        <Controls:DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
              ...
            </DataTemplate>
        </Controls:DataGridTemplateColumn.CellTemplate>
        <Controls:DataGridTemplateColumn.CellEditingTemplate>
            <DataTemplate>
                <ComboBox DisplayMemberPath="ShortName"   
                          SelectedItem="{Binding Path=Officer, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}"
                          ItemsSource="{Binding DataContext.Officers, ElementName=bridge}" />

            </DataTemplate>
+4

Source: https://habr.com/ru/post/1772251/


All Articles