Ray*_*Ray 17 silverlight xaml mvvm
最近,我意识到MVVM模式对Silverlight应用程序非常有用,并研究如何将它应用到我的项目中.
顺便说一句,如何用Command连接文本框的textChanged事件?Button有Command属性,但Textbox没有commapd属性.如果Controls没有命令属性,如何组合ICommand和Control的事件?
我得到了以下xaml代码
<UserControl.Resources>
<vm:CustomerViewModel x:Key="customerVM"/>
</UserControl.Resources>
<Grid x:Name="LayoutRoot"
Background="White"
DataContext="{Binding Path=Customers, Source={StaticResource customerVM}, Mode=TwoWay}" >
<StackPanel>
<StackPanel Orientation="Horizontal"
Width="300"
HorizontalAlignment="Center">
<TextBox x:Name="tbName"
Width="50"
Margin="10"/>
<Button Width="30"
Margin="10"
Content="Find"
Command="{Binding Path=GetCustomersByNameCommand, Source={StaticResource customerVM}}"
CommandParameter="{Binding Path=Text, ElementName=tbName}"/>
</StackPanel>
<sdk:DataGrid ItemsSource="{Binding Path=DataContext, ElementName=LayoutRoot}"
AutoGenerateColumns="True"
Width="300"
Height="300"/>
</StackPanel>
</Grid>
Run Code Online (Sandbox Code Playgroud)
我想要做的是,如果用户在文本框中输入一些文本,数据将显示在数据网格中而不是使用按钮单击.我知道内置了自动完成框控件.但是,我想知道如何在没有Command属性的控件中调用ViewModel类中的Command属性,例如textbox.
谢谢
Ken*_*art 23
为什么不将Text属性绑定到视图模型上的属性?这样,您可以在更改时收到通知,并获得新值:
public string MyData
{
get { return this.myData; }
set
{
if (this.myData != value)
{
this.myData = value;
this.OnPropertyChanged(() => this.MyData);
}
}
}
Run Code Online (Sandbox Code Playgroud)
XAML:
<TextBox Text="{Binding MyData}"/>
Run Code Online (Sandbox Code Playgroud)
Jer*_*ess 20
这是最简单的方法.如上所述,将文本框绑定到视图模型上的属性.然后,只需在文本框中添加一个代码隐藏(是的,我正在与MVVM进行代码隐藏,它不是世界末日)事件.添加TextChanged事件,然后只需刷新绑定.
总而言之,对于视图模型,您将拥有类似的内容:
public class MyViewModel
{
private string _myText;
public string MyText
{
get { return _myText; }
set
{
_myText = value;
RaisePropertyChanged("MyText"); // this needs to be implemented
// now do whatever grid refresh/etc
}
}
}
Run Code Online (Sandbox Code Playgroud)
在你的XAML中,你将拥有:
<TextBox Text="{Binding MyText,Mode=TwoWay}" TextChanged="TextBox_TextChanged"/>
Run Code Online (Sandbox Code Playgroud)
最后,在后面的代码中,只需执行以下操作:
public void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
var binding = ((TextBox)sender).GetBindingExpression(TextBox.TextProperty);
binding.UpdateSource();
}
Run Code Online (Sandbox Code Playgroud)
这将导致您的属性在文本更改时随时更新.}
这是MvvmLight的做法!归功于GalaSoft Laurent Bugnion.
<sdk:DataGrid Name="dataGrid1" Grid.Row="1"
ItemsSource="{Binding Path=CollectionView}"
IsEnabled="{Binding Path=CanLoad}"
IsReadOnly="True">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<cmd:EventToCommand
Command="{Binding SelectionChangedCommand}"
CommandParameter="{Binding SelectedItems, ElementName=dataGrid1}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</sdk:DataGrid>
Run Code Online (Sandbox Code Playgroud)
资料来源: http ://blog.galasoft.ch/archive/2010/05/19/handling-datagrid.selecteditems-in-an-mvvm-friendly-manner.aspx
只需使用
<TextBox Text="{Binding MyText,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
Run Code Online (Sandbox Code Playgroud)
小智 6
在定义部分,我们添加:
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
Run Code Online (Sandbox Code Playgroud)
如果您使用TextBox,请添加对我们要检测的事件的引用:
<TextBox Text="{Binding TextPrintersFilter}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="TextChanged">
<i:InvokeCommandAction Command="{Binding FilterTextChangedCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
Run Code Online (Sandbox Code Playgroud)
在ViewModel中添加Commad的代码:
public ICommand FilterTextChangedCommand
{
get
{
if (this._filterTextChangedCommand == null)
{
this._filterTextChangedCommand =
new RelayCommand(param => this.OnRequestFilterTextChanged());
}
return this._filterTextChangedCommand;
}
}
private void OnRequestFilterTextChanged()
{
// Add code
}
Run Code Online (Sandbox Code Playgroud)
不要忘记执行绑定文本:
private string _textPrintersFilter;
public string TextPrintersFilter
{
get { return _textPrintersFilter; }
set
{
_textPrintersFilter = value;
this.RaisePropertyChange(nameof(TextPrintersFilter));
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
51737 次 |
| 最近记录: |