如何为< textbox>使用两个绑定?在MVF与MVVM? [英] How can I use two binding for <textbox> in WPF with MVVM?
问题描述
我正在为< ListView>编写添加,更新和删除操作在WPF中使用MVVM。
我的问题是如何为相同的< TextBox>提供两个绑定(其中一个用于向< ListView>添加元素,另一个用于更新(selectItem of< ListView>))
我尝试过:
当我写入绑定以进行添加时,它成功运行。当我编写用于更新的绑定时,所选行被写入< TextBox>。但我需要这些工作。我试过< MultiBinding>但结果并不成功。希望你的帮助..
添加:
I'm writing adding,updating and deleting operations for <ListView> in WPF using by MVVM.
My question is that How can I give two binding for the same <TextBox>(one of these is for adding element to <ListView>and the other is for updating(selectedItem of <ListView>))
What I have tried:
When I write binding for adding it works successfully.And also When I write binding for updating the selected row is written to <TextBox>.But I need these work together.I tried <MultiBinding> but result weren't be successfull.Hope your helps..
For adding:
<TextBox Grid.Column="1" x:Name="txtSurname" Text="{Binding Path=Student.Surname, Mode=TwoWay}">
<TextBox.Effect>
<DropShadowEffect ShadowDepth="3" Color="#75B07A"/>
</TextBox.Effect>
</TextBox>-->
更新:
For updating:
<TextBox Grid.Column="1" x:Name="txtSurname" Text="{Binding ElementName=lstStuds, Path=SelectedItem.Surname}">
<TextBox.Effect>
<DropShadowEffect ShadowDepth="3" Color="#75B07A"/>
</TextBox.Effect>
</TextBox>
这是我的< listview> :
This is my <listview> :
<ListView x:Name="lstStuds" ItemsSource="{Binding Path=Students}" SelectedItem="{Binding SelectedStudent}" Grid.Column="1" Grid.Row="1" Grid.RowSpan="3" Margin="0,10,20,20" FontSize="16">
<ListView.Effect>
<DropShadowEffect ShadowDepth="3" Color="#75B07A"/>
</ListView.Effect>
<ListView.View>
<GridView>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" Width="150"/>
<GridViewColumn Header="Surname" DisplayMemberBinding="{Binding Surname}" Width="150"/>
</GridView>
</ListView.View>
我的按钮:
And my buttons:
<Button Content="Add" Command="{Binding AddCommand}" Width="120" Height="auto" FontSize="18" Margin="0 0 0 10">
<Button.Effect>
<DropShadowEffect ShadowDepth="3" Color="#75B07A"/>
</Button.Effect>
</Button>
<Button Content="Update" Command="{Binding EditCommand}" CommandParameter="{Binding ElementName=lstStuds, Path=SelectedItem}" Width="120" Height="auto" FontSize="18" Margin="0 0 0 10">
<Button.Effect>
<DropShadowEffect ShadowDepth="3" Color="#75B07A"/>
</Button.Effect>
</Button>
推荐答案
绑定到实体(模型),而不是选定项目 (观点部分);当数据源为空时为null。
添加时,实体是一个新对象(要添加到数据源);否则,它是来自数据源的现有项目。
你的MVVM正在流血;所以不要称之为......
You bind to an "entity" (model), not the "selected item" (a view part); which is null when the data source is empty.
When adding, the entity is a new object (to be added to the data source); otherwise, it's an existing item from the data source.
Your "MVVM" is bleeding; so don't call it that ...
这篇关于如何为< textbox>使用两个绑定?在MVF与MVVM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!