我正在使用XAML进行MVVM项目。在具有ItemSource绑定的ListView内进行操作时,访问外部元素的属性时遇到问题。
XAML:
<ListView x:Name="BibTexFields" Height="422" ItemsSource="{Binding BibTexFields}">
<ListView.ItemTemplate>
<DataTemplate>
<Grid Width="450">
<TextBlock Foreground="Black" Text="{Binding Field.Name}"/>
<CheckBox HorizontalAlignment="Right" IsChecked="{Binding IsChecked, Mode=TwoWay}" Command="{Binding UpdateFilledFieldsCommand}"/>
BibTexFields
是我的ViewModel的属性,它也是我的DataContext。也是UpdateFilledFieldsCommand
XAML:
xmlns:vm="using:StudyConfigurationClient.ViewModels"
mc:Ignorable="d" d:DataContext="{d:DesignInstance vm:CreateStudyViewModel}">
<Grid x:Name="FieldOuter">
<Border BorderBrush="Gray" BorderThickness="2" Margin="0, 0, 5, 0">
ViewModel:
private ObservableCollection<ChosenField> _bibTexFields;
public ObservableCollection<ChosenField> BibTexFields
{
get { return _bibTexFields; }
set
{
_bibTexFields = value;
OnPropertyChanged();
}
}
我想做的是ViewModel
从中访问ListView
,这样我就可以Command
对UpdateFilledFieldsCommand
属性进行绑定了。
我曾尝试研究RelativeSource
绑定,但似乎无法使其起作用。也没有尝试将其绑定到DataContext
工作。
您也可以使用ElementName:
<CheckBox HorizontalAlignment="Right" IsChecked="{Binding IsChecked, Mode=TwoWay}" Command="{Binding DataContext.UpdateFilledFieldsCommand, ElementName=BibTexFields}"/>
但是,RelativeSource也应该起作用。通过快速搜索,我发现:如何将WPF绑定与RelativeSource一起使用?看起来不错,尽管我不知道UWP应用程序中是否存在所有功能。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句