【发布时间】:2018-02-05 23:40:47
【问题描述】:
我有一个显示自定义user control、content control 和textbox...将视图更改为来自数据模板
我遇到的问题:我无法正确检索在交换的user control 上公开的基础依赖项属性。例如,每个用户控件都公开了一个IsSearching 依赖属性。
基于该值,我想禁用某些功能,直到 IsSearching 完成。我尝试了几种方法设置textbox 文本,但找不到检索绑定的正确方法。
我还尝试将依赖属性绑定到主视图模型 (CTALightViewModel) 上的属性,但它似乎无法正常工作。在这方面肯定有点迷失,所以任何帮助表示赞赏。
<views:CTAAddress x:Name="CTAAddressView" IsSearching="{Binding VMBusy, Mode=OneWay}"/>
数据模板
<Window.Resources>
<DataTemplate x:Key="AddressTemplate" DataType="{x:Type viewmodel:CTAAddressViewModel}">
<views:CTAAddress />
</DataTemplate>
<DataTemplate x:Key="PremiseTemplate" DataType="{x:Type viewmodel:CTAPremiseViewModel}">
<views:CTAPremise />
</DataTemplate>
</Window.Resources>
<Window.DataContext>
<viewmodel:CTALightViewModel />
</Window.DataContext>
内容控制
<ContentControl x:Name="ViewSwap" Content="{Binding }">
<ContentControl.Style>
<Style TargetType="{x:Type ContentControl}">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=SearchOptions, Path=IsSelected}" Value="0">
<Setter Property="ContentTemplate" Value="{StaticResource AddressTemplate}" />
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=SearchOptions, Path=IsSelected}" Value="1">
<Setter Property="ContentTemplate" Value="{StaticResource PremiseTemplate}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
要显示的文本框
<TextBox Text="{Binding ElementName=ViewSwap, Path=?????, Mode=OneWay}" />
【问题讨论】:
标签: c# wpf mvvm datatemplate contentcontrol