How to reference DependencyProperty which is Object in UserControl
I've got a UserControl that has a DependencyProperty declared as follows:
Code Snippet
publicstaticDependencyProperty DataObjectSessionsProperty =
DependencyProperty.Register("DataObjectSessions",typeof (SessionsODS.DataObjectSessions),
typeof (SessionView),null);
SessionsODS.DataObjectSession is a class with public properties which are the attributes I want to get at.
In the parent control (the one that instantiates the usercontrol), I have the following code, which works fine for my DependecyProperties that are simple string's, but I can't figure out how to pass the one above which is an object.
Code Snippet
<
ObjectDataProvider x:Key="SessionsODSDS" MethodName="GetAllSessions" d:IsDataSource="True" ObjectType="{x:Type WPF07:SessionsODS}"/><DataTemplate x:Key="SessionsODSTemplate"><WPF07:SessionView MyName="{Binding Path=ActualWidth, ElementName=DockPanel, Mode=OneWay}"SessionDescription="{Binding Path=Description}"SessionTitle="{Binding Path=Title}"SessionPresenter="{Binding Path=Username}"></< FONT>WPF07:SessionView></< FONT>DataTemplate>... and the listbox using this template sets the ItemSource as follows:
<
ListBox IsSynchronizedWithCurrentItem="True" ItemTemplate="{DynamicResource SessionsODSTemplate}"ItemsSource="{Binding Mode=OneWay, Source={StaticResource SessionsODSDS}}" d:UseSampleData="True"Width="Auto" Height="Auto"/> Then, in my actual UserControl, I want to reference public attributes of the object. I can access strings dependency properties like this:
Code Snippet
<
TextBlock x:Name="SessionViewTitle"Text="{Binding Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=SessionTitle}"FontSize="10" FontWeight="Bold"TextWrapping="Wrap" Margin="5,0,5,0" /> But I can't figure out how I would reference attributes of the object I'm passing in.
Basically, I'm wanting to pass a reference to the dataobject that is known in my parent control, and set the DataContext of the usercontrol to that object (which I've passed in as a dependencyproperty).
Clear? If not, ask and I'll try to explain more.
Thanks.
I'm a bit confused as to what you are trying to do.
Right now, the DataContext of your SessionView object is already set to your instance of SessionDataObject, by the DataTemplate system.
If you are trying to have your dependency property set to reference that same obect, you could do the following in your datatemplate:
Code Snippet
<DataTemplate x:Key="SessionsODSTemplate">
<WPF07:SessionView MyName="{Binding Path=ActualWidth, ElementName=DockPanel, Mode=OneWay}"
SessionDescription="{Binding Path=Description}"
SessionTitle="{Binding Path=Title}"
SessionPresenter="{Binding Path=Username}"
DataObjectSession="{Binding}"
>
</WPF07:SessionView>
</DataTemplate>
I believe you understand (though I mistakenly made DataObjectSessions plural even thought it is just one instance).
When I do what you suggest, I can't seem to get DataContext to set to it. I tried it in both the constructor and loaded events. I see in debug that DataObjectSessions is null.
Here is my usercontrol. (notice the three commented out lines of code) :
//SessionsODS.DataObjectSessions dos = new SessionsODS.DataObjectSessions();
//dos.Description = "mydescription"; //DataContext = dos; If I put these in, then DataContext works so I'm confident I'm referencing DataContext correctly, but just can't seem to get a reference to it in SessionView even though I pass it in as you suggest in dark blue above.
Code Snippet
namespace
WPF07 {
public partial class SessionView {
// used for setting width of this (need to change MyNameProperty to WidthSessionViewProperty) public static DependencyProperty DataObjectSessionsProperty = DependencyProperty.Register("DataObjectSessions", typeof (SessionsODS.DataObjectSessions), typeof (SessionView), null); public SessionView() {
InitializeComponent();
this.Loaded += new RoutedEventHandler(SessionView_Loaded); //SessionsODS.DataObjectSessions dos = new SessionsODS.DataObjectSessions(); //dos.Description = "mydescription"; //DataContext = dos; DataContext = DataObjectSessions;
}
void SessionView_Loaded(object sender, RoutedEventArgs e) {
DataContext = DataObjectSessions;
}
public SessionsODS.DataObjectSessions DataObjectSessions {
get {
return (SessionsODS.DataObjectSessions)GetValue(DataObjectSessionsProperty); }
set {
SetValue(DataObjectSessionsProperty,
value); }
}