WPF MVVM c#绑定文本框

时间:2014-10-30 17:01:30

标签: c# wpf mvvm binding

我有一个视图:listview with grid columns其中一列是textBox。 我需要一些如何能够从ViewModel绑定它。 有办法吗?

<ListView.View>
    <GridView x:Name="activeContents" AllowsColumnReorder="False">
         <GridViewColumn Width="30" Header=" ">
             <GridViewColumn.CellTemplate>
                  <DataTemplate>
                      <Image Source="{Binding IconFileFullPath}" Width="16" Height="16" ></Image>
                  </DataTemplate>
             </GridViewColumn.CellTemplate>
         </GridViewColumn>
    <GridViewColumn Width="150"  Header="Content" DisplayMemberBinding="{Binding name}"/>
    <GridViewColumn Width="125"  Header="Content Category"  DisplayMemberBinding="{Binding contentCategory}"/>
    <GridViewColumn Width="125"  Header="Content Version" DisplayMemberBinding="{Binding version}"/>
    <GridViewColumn Width="85"  Header="Status" DisplayMemberBinding="{Binding status}"/>
    <GridViewColumn Width="90" Header="Priority ">
         <GridViewColumn.CellTemplate>
              <DataTemplate>
                  <TextBox Text="{Binding seq, Mode=TwoWay}"  Width="67" />
              </DataTemplate>
         </GridViewColumn.CellTemplate>
    </GridViewColumn>
    </GridView>
</ListView.View>

尝试绑定但没有效果。 阅读有关处理行为但不知道如何做的事情。有一种简单的方法吗?

更多代码:

<!-- Active Contents-->
                            <ListView x:Name="ContentsList" SelectedItem="{Binding Path=contentToAction}" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3" ItemsSource="{Binding Path = activeContents, Mode= TwoWay}" HorizontalAlignment="Stretch" Margin="0,6,0,0" VerticalAlignment="Top" Background="#FF454545"
                                       SelectionMode="Single" Height="130" Width="650" ScrollViewer.CanContentScroll="True"
                                       dd:DragDrop.IsDragSource="True" dd:DragDrop.IsDropTarget="True" dd:DragDrop.DropHandler="{Binding}" dd:DragDrop.UseDefaultDragAdorner="True">
                                <ListView.Resources>
                                    <ContextMenu x:Key="ItemContextMenu">
                                        <MenuItem Header="Open in CM" Command="{Binding DataContext.OpenContentVersionInCm,   RelativeSource={RelativeSource AncestorType={x:Type ListView}}}"/>
                                        <MenuItem Header="Execute" Command="{Binding DataContext.ContentCopyCommand,   RelativeSource={RelativeSource AncestorType={x:Type ListView}}}"/>
                                        <MenuItem Header="Update Execution Priority"  Command="{Binding DataContext.UpdateExecutionPriorityCommand, RelativeSource={RelativeSource AncestorType={x:Type ListView}}}"/>
                                    </ContextMenu>
                                    <Style TargetType="GridViewColumnHeader">
                                        <Setter Property="IsEnabled" Value="True"/>
                                        <Setter Property="TextElement.Foreground" Value="Black"/>
                                    </Style>
                                </ListView.Resources>



                                <ListView.View>
                                    <GridView x:Name="activeContents" AllowsColumnReorder="False">

                                        <GridViewColumn Width="30" Header=" ">
                                            <GridViewColumn.CellTemplate>
                                                <DataTemplate>
                                                    <Image Source="{Binding IconFileFullPath}" Width="16" Height="16" ></Image>
                                                </DataTemplate>
                                            </GridViewColumn.CellTemplate>
                                        </GridViewColumn>
                                        <GridViewColumn Width="150"  Header="Content" DisplayMemberBinding="{Binding name}"/>
                                        <GridViewColumn Width="125"  Header="Content Category"  DisplayMemberBinding="{Binding contentCategory}"/>
                                        <GridViewColumn Width="125"  Header="Content Version" DisplayMemberBinding="{Binding version}"/>
                                        <GridViewColumn Width="85"  Header="Status" DisplayMemberBinding="{Binding status}"/>
                                        <GridViewColumn Width="90" Header="Priority ">
                                            <GridViewColumn.CellTemplate>
                                                <DataTemplate>
                                                    <TextBox Text="{Binding seq, Mode=TwoWay}"  Width="67" />
                                                </DataTemplate>
                                            </GridViewColumn.CellTemplate>
                                        </GridViewColumn>
                                    </GridView>
                                </ListView.View>

                                <ListView.ItemContainerStyle>

1 个答案:

答案 0 :(得分:1)

进入GridViewColumn后,您的DataContext将更改为绑定对象。

这意味着,如果它有一个属性seq;你的绑定将正常工作。但是,如果您希望视图模型本身具有属性seq,则有两个选项。无论哪种方式,您的路径都变为:

"{Binding Path=DataContext.seq}"

首先是命名你的窗口&#34; root&#34;并执行ElementName绑定:

"{Binding Path=DataContext.seq, ElementName=root}"

另一种是使用RelativeSource绑定:

"{Binding Path=DataContext.seq, RelativeSource={RelativeSource AncestorType={x:Type Window}}"