3-1*_*264 5 c# wpf xaml system.reactive reactiveui
使用 ReactiveUI 时,可以在 xaml 中设置绑定...
<TextBox x:Name="SomeProperty" Text="{Binding SomeProperty}" />
Run Code Online (Sandbox Code Playgroud)
或在后面的代码中
this.Bind(ViewModel, vm.SomeProperty, v => v.SomeProperty.Text);
Run Code Online (Sandbox Code Playgroud)
似乎在某些情况下(例如绑定到 ListBox 中的子对象),使用该.xaml选项似乎是唯一的方法
例如
<ListView>
<ListView.ItemTemplate>
<DataTemplate DataType="ListViewItem">
<TextBox Text="{Binding ChildViewModelProperty}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Run Code Online (Sandbox Code Playgroud)
我用错了{Binding }还是我可以混合搭配.xaml,code behind我认为合适?!
我认为您可能(几乎)不使用 XAML 绑定就可以摆脱困境,但需要编写更多代码。我将使用 Ana\xc3\xafs Betts\' XamarinEvolve2014 演示作为示例。
\n首先,您需要为列表中的项目定义一个 ViewModel (类似于LoginTeamTileViewModel):
\npublic class TileViewModel : ReactiveObject\n{\n string name;\n public string Name {\n get { return name; }\n set { this.RaiseAndSetIfChanged(ref name, value); }\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n然后,您需要在 ViewModel 中公开这些类的集合 -例如作为ReactiveList:
public class ViewModel : ReactiveObject\n{\n public ReactiveList<TileViewModel> Tiles { get; private set; }\n}\nRun Code Online (Sandbox Code Playgroud)\n您可以使用ReactiveDerivedCollection您的模型创建这些 ViewModel。
接下来,您将创建一个简单的视图TileViewModel,类似于 Evolve 示例中的视图。
最后,您应该在列表视图中使用创建的视图作为数据模板,如示例中所示。请注意,它仍然使用{Binding}, 但仅用于 ViewModel 属性,而不用于单独的字段,这看起来更干净。(遗憾的是,我已经有一段时间没有编写任何 WPF 了,所以我无法在这里快速编写任何示例 - 欢迎编辑!)。
在后面的代码中,您应该将集合绑定到ItemsSourceListView 的 ,如下所示:
this.OneWayBind(ViewModel, vm => vm.Tiles, v => v.ListView.ItemsSource);
希望这可以帮助!
\n| 归档时间: |
|
| 查看次数: |
3842 次 |
| 最近记录: |