简体   繁体   中英

WPF MVVM c# binding textbox

I have a View in it : listview with grid columns one of the columns is a textBox. I need to some how be able to binding it From the ViewModel. is there a way to do it ?

<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>

tried to binding but no effect . read something about working with behavior but don't know how to do that. is there an easy way ?

more code :

<!-- 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>

Once you are inside the GridViewColumn your DataContext changes to the bound object.

That means, if it has a property seq ; your binding will work correctly. If you wanted the property seq on the view model itself however, you have two options. Either way, your path becomes:

"{Binding Path=DataContext.seq}"

The first is to name your window "root" and do an ElementName binding:

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

The other is to use a RelativeSource binding:

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

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