我有一个窗口显示自定义user control
, content control
和 textbox
...当用户在自定义user control
上选择radio button
时视图来自数据模板
我遇到的问题是:我无法正确检索交换user control
上暴露的基本依赖性属性。例如,每个用户控件都会公开IsSearching
依赖项属性。
基于值,我想禁用一些功能,直到IsSearching
完成。我尝试设置textbox
文本几种方式,但找不到检索绑定的正确方法。
我还尝试将依赖项属性绑定到MainViewModel(CtalightViewModel(上的属性,但似乎无法正常工作。绝对有些丢失,因此任何帮助都得到赞赏。
<views:CTAAddress x:Name="CTAAddressView" IsSearching="{Binding VMBusy, Mode=OneWay}"/>
dataTemplates
<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}" />
由于您与CTALightViewModel
绑定,您是否在CTALightViewModel
中有一个称为IsSearching
的属性,您可以将其设置为绑定中的路径?例如
<TextBox Text="{Binding ElementName=ViewSwap, Path=?????, Mode=OneWay}" />
如果不是,则可以在CTALightViewModel
中创建称为IsSearching
的属性,在属性的设置器中,您可以调用OnPropertyChanged()
,以便在发生更改时学习ui。
我意识到我需要实现一个视图模型基础来实现这一目标。我创建了一个,并且其他视图模型从此基础上继承了。