我是MVVM的新手.学习我创建了一个示例应用程序,以便在单击按钮时在文本框中显示消息.在我的代码中,button命令工作正常,但属性未绑定到Textbox.如何使用MVVM将Property绑定到Textbox?
我的代码类似于下面给出的.
视图
<TextBox Name="MessageTextBox" Text="{Binding TestMessage}"/>
<Button Content="Show" Name="button1" Command="{Binding ShowCommand}">
<!-- Command Handler -->
</Button>
Run Code Online (Sandbox Code Playgroud)
查看模型
MyMessage myMessage;
public MainViewModel()
{
myMessage=new MyMessage();
}
//inside the ShowCommand Handler
TestMessage="Hello World";
// A Property to set TextBox Value.
Run Code Online (Sandbox Code Playgroud)
模型
public class MyMessage: INotifyPropertyChanged
{
private string testMessage;
public string TestMessage
{
get { return testMessage; }
set
{
testMessage= value;
OnPropertyChanged("TestName");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new …
Run Code Online (Sandbox Code Playgroud) 有人可以解释MVVM Foundation和MVVM Toolkit之间的差异吗?他们似乎有很多共同之处.
我正在使用MVVM工具包版本1.我有两个文本框textbox1和textbox2.我需要在按下按钮时将这两个值作为参数传递,并且需要在名为textbox3的第三个文本框上显示结果.
我的VM代码与此类似
public ICommand AddCommand
{
get
{
if (addCommand == null)
{
addCommand = new DelegateCommand<object>(CommandExecute,CanCommandExecute);
}
return addCommand;
}
}
private void CommandExecute(object parameter)
{
var values = (object[])parameter;
var a= (int)values[0];
var b= (int)values[1];
Calculater calcu = new Calcu();
int c = calcu.sum(a, b);
}
private bool CanCommandExecute(object parameter)
{
return true;
}
Run Code Online (Sandbox Code Playgroud)
当用户单击按钮但我的参数参数没有任何值时,将调用commandExecute方法.我如何将用户的值作为参数传递?并将结果返回到texbox3?
我正面临ListBox的ItemsSource相关问题.我正在使用WPF MVVM工具包版本0.1实现MVVM.
当用户双击某个其他元素时,我将一个ListBox itemSource设置为更新(我在后面的代码中处理了事件并在那里执行了命令,因为不支持将命令绑定到特定事件).此时,通过执行命令,将生成一个新的ObservableCollection项,并且ListBox的ItemsSource将相应地更新.但目前还没有发生.ListBox不会动态更新.可能是什么问题?我附上了relvent代码供你参考.
XAML:
双击项目列表以生成下一个列表:
<ListBox Height="162" HorizontalAlignment="Left" Margin="10,38,0,0" Name="tablesViewList" VerticalAlignment="Top" Width="144" Background="Transparent" BorderBrush="#20EEE2E2" BorderThickness="5" Foreground="White" ItemsSource="{Binding Path=Tables}" SelectedValue="{Binding TableNameSelected, Mode=OneWayToSource}" MouseDoubleClick="tablesViewList_MouseDoubleClick"/>
Run Code Online (Sandbox Code Playgroud)
目前尚未更新的第二个项目列表:
<ListBox Height="153" HorizontalAlignment="Left" Margin="10,233,0,0" Name="columnList" VerticalAlignment="Top" Width="144" Background="Transparent" BorderBrush="#20EEE2E2" BorderThickness="5" Foreground="White" ItemsSource="{Binding Path=Columns, Mode=OneWay}" DisplayMemberPath="ColumnDiscriptor"></ListBox>
Run Code Online (Sandbox Code Playgroud)
代码背后:
private void tablesViewList_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
MainViewModel currentViewModel = (MainViewModel)DataContext;
MessageBox.Show("Before event command is executed");
ICommand command = currentViewModel.PopulateColumns;
command.Execute(null);
MessageBox.Show(currentViewModel.TableNameSelected);
//command.Execute();
}
Run Code Online (Sandbox Code Playgroud)
查看型号:
namespace QueryBuilderMVVM.ViewModels
{
//delegate void Del();
public class MainViewModel : ViewModelBase
{
private DelegateCommand exitCommand; …
Run Code Online (Sandbox Code Playgroud) 嗨,我正在尝试使用带有MVVM的telerik Busy指示器.我在Mainwindow有忙碌指示器.当窗口中的某个用户控件上有操作(按钮单击)时,用户控件视图模型会向MinwindowviewModel发送消息.在消息上,应该显示忙碌指示符.但这不起作用.为什么这不起作用?
用户控制视图模型
public class GetCustomerVM : ViewModelBase
{
private int _CustomerId;
public int CustomerId
{
get { return _CustomerId; }
set
{
if (value != _CustomerId)
{
_CustomerId = value;
RaisePropertyChanged("CustomerId");
}
}
}
public RelayCommand StartFetching { get; private set; }
public GetCustomerVM()
{
StartFetching = new RelayCommand(OnStart);
}
private void OnStart()
{
Messenger.Default.Send(new Start());
AccountDetails a = AccountRepository.GetAccountDetailsByID(CustomerId);
Messenger.Default.Send(new Complete());
}
}
Run Code Online (Sandbox Code Playgroud)
用户控制视图模型是:
private bool _IsBusy;
public bool IsBusy
{
get { return _IsBusy; }
set
{ …
Run Code Online (Sandbox Code Playgroud) 我是初学者,谈到OOP.昨天我试图阅读一些mvvm/wpf示例,当然我遇到了麻烦...我有一些问题,了解下面的一些代码:
{
class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Addres { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这只是普通的Person类,这里没什么特别的.问题是我无法理解下面的代码:
private void SayHi_Click(object sender, RoutedEventArgs e)
{
Person person = new Person
{
FirstName=FirstName.Text,
LastName=LastName.Text,
Addres=Address.Text
};
Run Code Online (Sandbox Code Playgroud)
我不理解的部分是:
Person person = new Person
{
FirstName=FirstName.Text,
LastName=LastName.Text,
Addres=Address.Text
};
Run Code Online (Sandbox Code Playgroud)
我不确定这究竟是什么.我认为每个新对象都应该像这样初始化:Class class = new Class();. 为什么"新人"之后没有()?而不是()我们有{}.我知道我们可以使用默认,参数化,静态和私有构造函数.有人可以向我解释一下吗?来自CodeProject的以下教程中的类似情况:
http://www.codeproject.com/Articles/165368/WPF-MVVM-Quick-Start-Tutorial
我们有一个歌曲课
public class Song
{
#region Members
string _artistName;
string _songTitle;
#endregion
#region Properties
/// The artist …
Run Code Online (Sandbox Code Playgroud) 通常需要在Model类,ViewModel类或两者上实现INotifyPropertyChanged吗?是否可以仅在Model上实现,而不是在Viewmodel上实现?如果不可能模型那么为什么
我正在使用在Raspberry Pi下运行的XAML开发通用Windows平台应用程序Windows 10 IoT Core
.该应用程序驱动I2C总线上的温度传感器.传感器类是MLX90614Thermometer
.传感器使用a DispatcherTimer
每100毫秒(大约)获取读数并更新移动平均值.当移动平均值变化超过指定阈值时,传感器会引发ValueChanged
事件并在事件args中提供新值.
在我的ViewModel类中TemperatureSensorViewModel
,我订阅了传感器的ValueChanged
事件并使用它来更新命名的绑定属性Ambient
,Channel1
和Channel2
.这些属性绑定到XAML UI中的文本块.这是事件处理程序:
void HandleSensorValueChanged(object sender, SensorValueChangedEventArgs e)
{
switch (e.Channel)
{
case 0:
Ambient = e.Value;
break;
case 1:
Channel1 = e.Value;
break;
case 2:
Channel2 = e.Value;
break;
}
}
Run Code Online (Sandbox Code Playgroud)
...这里是一个示例数据绑定Ambient
...
<TextBlock x:Name="Ambient" Grid.Row="1" Text="{Binding Path=Ambient}" Style="{StaticResource FieldValueStyle}" />
Run Code Online (Sandbox Code Playgroud)
我正在使用MVVM Light Toolkit,因此我的属性就像这样实现(仅Ambient
显示,但除了名称之外其他都是相同的):
public double Ambient
{
get { return ambientTemperature; …
Run Code Online (Sandbox Code Playgroud) xaml inotifypropertychanged mvvm-toolkit windows-10-iot-core uwp
我正在尝试通过 Converter 绑定 WPF 中窗口的可见性。我收到错误。系统.Windows.StaticResourceExtension 系统.Windows.StaticResourceExtension
我在下面提供我的代码。我的视图是 在此处输入图像描述
<Window x:Class="UI.ChildWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:UI"
xmlns:UtilityValue="clr-namespace:UI.Utility"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
mc:Ignorable="d"
Title="ChildWindow" Height="70" Width="400" WindowStartupLocation="CenterScreen" WindowStyle="None"
Visibility="{Binding WindowVisibility, Converter={StaticResource VisibilityConverter}, Mode=TwoWay}">
<Window.Resources>
<UtilityValue:TextInputToVisibilityConverter x:Key="TextInputToVisibilityConverter"></UtilityValue:TextInputToVisibilityConverter>
<UtilityValue:EventToCommandBehavior x:Key="CommandBehavior"></UtilityValue:EventToCommandBehavior>
<SolidColorBrush x:Key="brushWatermarkBackground" Color="White" />
<SolidColorBrush x:Key="brushWatermarkForeground" Color="LightSteelBlue" />
<SolidColorBrush x:Key="brushWatermarkBorder" Color="Indigo" />
<UtilityValue:BooleanToVisibilityConverter x:Key="VisibilityConverter"></UtilityValue:BooleanToVisibilityConverter>
<Style x:Key="EntryFieldStyle" TargetType="Grid" >
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="Margin" Value="2" />
</Style>
</Window.Resources>
Run Code Online (Sandbox Code Playgroud)
我的视图模型如下:
private bool _windowVisibility=true;
public bool WindowVisibility
{
get { return _windowVisibility; }
set …
Run Code Online (Sandbox Code Playgroud) 我有一个ViewModel
接受多个构造函数参数的。据我了解,因此设置 View 的 DataContext 的唯一方法是使用隐藏代码。
这有其缺点:
ViewModel
正在构造的视图的智能ViewModel
在 XAML 设计器中查看我的构造函数中定义的设计时数据,因为设计器只是中断了我有什么选择?
我希望有一个ViewModel
可以接受构造函数参数,具有设计时数据,并且我的 Visual Studio 智能为我提供有关我的成员的建议ViewModel
,以便我可以获得良好的设计体验。
附言。我正在使用Microsoft 的MVVM Toolkit / Windows Community Toolkit,但如果您能提供有关如何实现我的最终目标的答案,我将不胜感激。谢谢。
mvvm-toolkit ×10
wpf ×8
mvvm ×7
c# ×3
xaml ×3
mvvm-light ×2
.net ×1
data-binding ×1
oop ×1
uwp ×1