UserControl's Resources as UserControl's DataContext?

房东的猫 提交于 2019-12-12 03:14:42

问题


Is it possible to give UserControl's Resources(Or one of its resource) as UserControl's DataContext?

I tried to bind a Button's Command Property in a DataGrid's CellTemplate to a property in my ViewModel.

If it were use a ListBox instead of DataGrid this works for me such as below,

 <ListBox.ItemTemplate>
    <DataTemplate>       
       <HyperlinkButton Content="{Binding DESCRIPTION}"                                                             
        Command="{Binding DataContext.SelectSingleBackCommand, ElementName=LayoutRoot}"/>
        </StackPanel>
    </DataTemplate>
</ListBox.ItemTemplate>

But if I use DataGrid instead of ListBox even I Clicked the button there is no effect!

<data:DataGrid x:Name="RadGridSearchResults" ItemsSource="{Binding SearchResults}"  AutoGenerateColumns="False" IsReadOnly="True">
    <data:DataGrid.Columns>     
        <data:DataGridTemplateColumn Header="Just Header" CanUserSort="True" SortMemberPath="DESCRIPTION">
            <data:DataGridTemplateColumn.CellTemplate>
                <DataTemplate>                                        
                    <Button Margin="5"  Content="{Binding DESCRIPTION}" Command="{Binding DataContext.SelectSingleBackCommand,ElementName=LayoutRoot}"/>
                </DataTemplate>
            </data:DataGridTemplateColumn.CellTemplate>
        </data:DataGridTemplateColumn>
    </data:DataGrid.Columns>
</data:DataGrid>

Then I thought there could be another LayoutRoot in DataGrid(Data Grid is a simple sl4 grid.) And changed LayoutRoot's name to LayoutRootMain. No way.

Note:Then I removed < UserControl.DataContext > part then,

So I decided to pass my ViewModel on < UserConrol.Resources > Part as shown below

<UserConrol.Resources>
    <modelview:SelectReceiversViewModel x:Key="MainDataContextResource" x:Name="MainDataContextResource"/>
</UserControl.Resources>

But How can I bind UserControl's DataContext property to this resource, I tried;

<UserControl...   DataContext="{Binding RelativeSource={RelativeSource Self},Path=Resources}"

Or

 <UserControl...  DataContext="{Binding ElementName=MainDataContextResource}"

Thanks!

来源:https://stackoverflow.com/questions/10171630/usercontrols-resources-as-usercontrols-datacontext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!