简体   繁体   中英

Get DataContext from UserControl

I have a user control whose datacontext is set with this:

<UserControl.Resources>
    <data:ArchivosViewModel x:Key="Data" />
</UserControl.Resources>

In this user control I want to load a grid view with a list of objects in 5 columns with several values (date, url, name... and a button to delete each row).

To add this button I want to use a command that I have in my UserControl ViewModel and a CommandParameter wich I'll have in my datagrid source datacontext.

I think the best way to do this would be to set the datacontext in that button to the staticresource but it is not working... Any idea of how to do this?

<sdk:DataGrid AutoGenerateColumns="False" Margin="5" ItemsSource="{Binding Ficheros, Mode=TwoWay, ValidatesOnDataErrors=True}" SelectionMode="Single"  Grid.Row="2" Height="Auto" HorizontalAlignment="Stretch" Name="dataGrid1" VerticalAlignment="Stretch" MinHeight="0" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" >
    <sdk:DataGrid.Columns>
        <sdk:DataGridTemplateColumn Header="Fecha">
            <sdk:DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <sdk:DatePicker SelectedDate="{Binding fecha_fichero, Mode=TwoWay}" Margin="5,0" Width="Auto"></sdk:DatePicker>
                </DataTemplate>
            </sdk:DataGridTemplateColumn.CellTemplate>
        </sdk:DataGridTemplateColumn>
        <sdk:DataGridTextColumn Binding="{Binding nombre_fichero, Mode=TwoWay}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Header="Nombre" Width="Auto" />
        <sdk:DataGridTextColumn Binding="{Binding descripcion_fichero, Mode=TwoWay}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Header="Descripción" Width="Auto" />
        <sdk:DataGridTemplateColumn Header="Enlace">
            <sdk:DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <HyperlinkButton Margin="5,0" Width="Auto" NavigateUri="{Binding ruta_fichero, Mode=OneWay}" TargetName="_blank" Content="Abrir Fichero"></HyperlinkButton>
                </DataTemplate>
            </sdk:DataGridTemplateColumn.CellTemplate>
        </sdk:DataGridTemplateColumn>
        <sdk:DataGridTemplateColumn Header="Eliminar">
            <sdk:DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Button Width="Auto" Margin="5,0" Content="Eliminar Fichero" Command="{Binding EliminarArchivoCommand}" ></Button>
                </DataTemplate>
            </sdk:DataGridTemplateColumn.CellTemplate>
        </sdk:DataGridTemplateColumn>
    </sdk:DataGrid.Columns>
</sdk:DataGrid>

Thanks in advance.

try:

 Command="{Binding EliminarArchivoCommand,Source={StaticResource Data}}" 

and:

CommandArgument="{Binding}"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM