ThePerfectStorm

If my DynamicsDataSource is intantiated in Window1 then accessed using DataContext, the object becomes completely local to window1 so I can do whatever I want to the ObservableCollection. But when using this method Expression blend is no longer able to show the data statically during GUI design.

I would like to use Expression blend but I cannot figure out how to access methods in the DynamicsDataSource object from Window1 code behind. The DynamicsDataSource object is instanciated only via XAML.

Is there a way to get the XAML instanciated DynamicsDataSource instance so that I can access it's methods

The basic functionality that I would like to achieve is being able to modify the Collection when selections are made.


The following codes snippet includes both methods tried. The DataContext method code is justed commented out.

// ############## Window1.xaml.cs
xmlns: DynamicsDataSource="clr-namespace: DynamicsDataSource;assembly=DynamicsDataSource"

<ObjectDataProvider x:Key="ProductLinesDS" d:IsDataSource="True" ObjectType="{x:Type DynamicsDataSource: ProductLines}"/>

<ListBox x:Name="ProductLineList" SelectionMode="Multiple"
SelectionChanged="onLineSelection"
ItemTemplate="{DynamicResource ProductLinesTemplate}"
ItemsSource="{Binding Mode=OneWay, Source={StaticResource ProductLinesDS}}"/>
<!-- DataContext Method ItemsSource="{Binding}" -->

// ############## Window1.cs
// ProductLines pl = new ProductLines();

public Window1()
{
this.InitializeComponent();
// this.DataContext = pl;
}

public void onLineSelection(object sender, SelectionChangedEventArgs e)
{
IList items = ProductLineList.SelectedItems;
foreach (Object item in items)
{
// MessageBox.Show(item.Name.ToString());
}

// I would like to access DynamicsDataSource Methods here but I need access to the XAML instantated object
// pl.Add(new ProductLine("FOOBAR"));
}

Thanks,
Jeff



Re: Windows Presentation Foundation (WPF) How to modify observableCollection object and still see data in Expression Blend?

Marco Zhou - MSFT

Hi Jeff,

You should be able to retrieve the "DynamicsDataSource" using the following code:

ObjectDataProvider provider = this.TryFindResource("ProductLinesDS") as ObjectDataProvider;

if (provider != null)

{

ProductLines pl = provider.Data as ProductLines;

}

Hope this helps





Re: Windows Presentation Foundation (WPF) How to modify observableCollection object and still see data in Expression Blend?

ThePerfectStorm

Thankyou for a great solution.