简体   繁体   English

绑定属性何时获得输入?

[英]When a bound property gets a input?

Currently im learning to build windows apps with mvvm with the Template Studio.目前我正在学习使用 Template Studio 使用 mvvm 构建 Windows 应用程序。 I added a SettingsPage what contains some elements like:我添加了一个 SettingsPage ,其中包含一些元素,例如:

<StackPanel x:Name="ContentArea">
        <CommandBar Background="Transparent" OverflowButtonVisibility="Collapsed">
            <AppBarButton Click="BtnSave_Click" Icon="Save" />          
        </CommandBar>
        <Pivot>
            <PivotItem x:Uid="Settings_Personal">
                <StackPanel Margin="{StaticResource XSmallTopMargin}">
                    <TextBlock x:Uid="Settings_Personal_Firstname" />
                    <TextBox x:Uid="Settings_Personal_FirstnameBox" Text="{x:Bind ViewModel.Firstname, Mode=TwoWay}" />                        
                </StackPanel>
            </PivotItem>
        <Pivot>
</StackPanel>

My SettingsPage.xaml.cs:我的 SettingsPage.xaml.cs:

public sealed partial class SettingsPage : Page
{
      public SettingsViewModel ViewModel { get; }

      public SettingsPage()
      {
         ViewModel = App.GetService<SettingsViewModel>();
         InitializeComponent();
      }

      public void BtnSave_Click(object sender, RoutedEventArgs routedEventArgs)
      {
         ViewModel.SetSetting();
      }
}

And my Viewmodel:还有我的视图模型:

public class SettingsViewModel : ObservableRecipient
{
private string _firstname;

public string Firstname
{
    get => _firstname;
    set => SetProperty(ref _firstname, value);
}

public SettingsViewModel(ILocalSettingsService localSettingsService)
{
    _localSettingsService = localSettingsService;
}

public async Task SetSetting()
{
    string test = Firstname;  // < My Breakpoint
    await _localSettingsService.SaveSettingAsync("Firstname", Firstname);
}

} }

At debug time, i'm filling the box with my firstname and set a brakpoint to the SetSettings Task.在调试时,我用我的名字填充该框,并为 SetSettings 任务设置一个制动点。 After clicking on the "Save" Button i can see, that Firstname is null.单击“保存”按钮后,我可以看到,Firstname 为空。 Do i have to add some more code for gettings the information from the box?我是否必须添加更多代码才能从框中获取信息?

由于{x:Bind}具有OneTime的默认模式,因此您应该将绑定的Mode设置为TwoWay ,以便您的源属性按预期设置:

Text="{x:Bind ViewModel.Firstname, Mode=TwoWay}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何知道MVVM绑定属性的内部属性何时被修改? - How to know when an inner property of a MVVM bound property gets modified? 将TextBox绑定到属性后,格式字符串将被忽略 - Format string is ignored after TextBox gets bound to a property 绑定到可见性属性(奇数)时,动画不正确 - Animation not correct when bound to a visibility property(Oddity) 以编程方式更改SelectedIndex时更改绑定属性 - Change bound Property when programmatically changing SelectedIndex 绑定到属性时标签文本不更新 - Label text not updating when bound to property WPF-属性更改时绑定控件未更新? - WPF - Bound Control Not Updating When Property Changed? 当各个属性绑定到ScrollBar时,不会触发PropertyChangedCallback - PropertyChangedCallback not firing when respective property bound to ScrollBar 绑定到的数据库更新时,datagridview是否可以自动更新? - is it possible to have a datagridview automatically update when the database it is bound to gets updated? 屏幕关闭时,Android绑定服务被冻结 - android Bound Service gets frozen when screen is off 当绑定数据更改时,用户控件上的依赖项属性不会更新属性 - Dependency Property on a user control not updating the property when bound data changes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM