从视图模型中获取列表中的选定项

时间:2012-02-21 09:42:32

标签: c# wpf mvvm wpfdatagrid

早上好!

我正在编写一个管理应用程序,它显示了使用WPF和MVVM模式的任务列表。用户也可以编辑此列表,但我想使用删除按钮删除所选行。问题是我不知道如何从ModelView访问所选行,我不想使用代码隐藏。我想最好的选择是使用一些绑定,但我不确定如何做到这一点。

这是代码(我使用的是DevXpress控件,但几乎相同)

<dxg:GridControl  ItemsSource="{Binding Path=TaskView}" Loaded="GridControl_Loaded">
       <dxg:GridControl.Columns>
            <dxg:GridColumn Visible="False" DisplayMemberBinding="{Binding Path=ID, Mode=OneWay}" />
            <dxg:GridColumn Header="Project" DisplayMemberBinding="{Binding Path=ProjectCode, Mode=TwoWay}" AllowEditing="True" ContentChanged="OnHoursChanged"/>
            <dxg:GridColumn Header="Project Description" DisplayMemberBinding="{Binding Path=ProjectName, Mode=TwoWay}" AllowEditing="False"/>
            <dxg:GridColumn Header="Description" DisplayMemberBinding="{Binding Path=ProjectDescription, Mode=TwoWay}" AllowEditing="True"/>
            <dxg:GridColumn Header="Comment" DisplayMemberBinding="{Binding Path=Comment, Mode=TwoWay}" AllowEditing="True"/>
            <dxg:GridColumn Header="{Binding ElementName=mon,Path=Text}" DisplayMemberBinding="{Binding Path=HoursMonday, Mode=TwoWay}" AllowEditing="True" FieldName="M" />
            <dxg:GridColumn Header="{Binding ElementName=tue,Path=Text}" DisplayMemberBinding="{Binding Path=HoursTuesday, Mode=TwoWay}" AllowEditing="True" FieldName="T" />
            <dxg:GridColumn Header="{Binding ElementName=wed,Path=Text}" DisplayMemberBinding="{Binding Path=HoursWednesday, Mode=TwoWay}" AllowEditing="True" FieldName="W" />
            <dxg:GridColumn Header="{Binding ElementName=thu,Path=Text}" DisplayMemberBinding="{Binding Path=HoursThursday, Mode=TwoWay}" AllowEditing="True" FieldName="Th" />
            <dxg:GridColumn Header="{Binding ElementName=fri,Path=Text}" DisplayMemberBinding="{Binding Path=HoursFriday, Mode=TwoWay}" AllowEditing="True" FieldName="F" />
            <dxg:GridColumn Header="{Binding ElementName=sat,Path=Text}" DisplayMemberBinding="{Binding Path=HoursSaturday, Mode=TwoWay}" AllowEditing="True" FieldName="Sat" />
            <dxg:GridColumn Header="{Binding ElementName=sun,Path=Text}" DisplayMemberBinding="{Binding Path=HoursSunday, Mode=TwoWay}" AllowEditing="True" FieldName="Sun" />
            <dxg:GridColumn Header="Total" DisplayMemberBinding="{Binding Path=Total, Mode=OneWay}" AllowEditing="False" FieldName="Tot" />
        </dxg:GridControl.Columns>
    </dxg:GridControl>

你有什么想法吗?

非常感谢!

2 个答案:

答案 0 :(得分:3)

您的虚拟机应具有Project SelectedProject { get; set; }属性。

从Grid到它的SelectedRow(或其他东西):

ItemsSource="{Binding Path=TaskView}" SelectedItem="{Binding Path=SelectedProject}"

答案 1 :(得分:0)

这样的事情:

<dxg:GridControl Grid.Row="5" Grid.Column="0" Grid.ColumnSpan="5"  Margin="4,4,4,4"
                             Name="gridDatos"
                             ItemsSource="{Binding Usuarios}" IsFilterEnabled="True"
                             >
                <dxg:GridControl.View>
                    <dxg:TableView AllowPerPixelScrolling="True" AllowEditing="False" NavigationStyle="Row"
                                   AllowHorizontalScrollingVirtualization="False"
                                   ShowGroupPanel="False"
                                   FocusedRow="{Binding SelectedUsuario, Mode=TwoWay}"/>
                </dxg:GridControl.View>
                <dxg:GridControl.Columns>
                    <dxg:GridColumn FieldName="usuario_us" ReadOnly="True" Header="Usuario" Width="133" />
                    <dxg:GridColumn FieldName="nombre_us"  ReadOnly="True" Header="Nombre" Width="308" />
                    <dxg:GridColumn FieldName="activo_us"  ReadOnly="True" Header="Activo" />
                </dxg:GridControl.Columns>
            </dxg:GridControl>