我对XAML相当陌生,但喜欢学习它。我真正努力的是将一个属性绑定到DataTemplate
中的一个元素。XAML:绑定DataTemplate中的属性
我已经创建了一个简单的WPF示例来(希望)解释我的问题。
我这个例子我试图将中CheckBox
的Visibility
属性绑定到我的viewmodel中的属性。 (纯粹用于学习/演示使用此场景。)
我有一个简单的DataModel,名称为Item
,但在此示例中没有多大关联。
class Item : INotifyPropertyChanged
{
// Fields...
private bool _IsRequired;
private string _ItemName;
和一个相当简单的名为ItemViewModel的视图模型。
class ItemViewModel : INotifyPropertyChanged
{
private ObservableCollection<Item> _Items;
private bool _IsCheckBoxChecked;
private bool _IsCheckBoxVisible;
public ObservableCollection<Item> Items
{
get { return _Items; }
set { _Items = value; }
}
public bool IsCheckBoxChecked
{
get { return _IsCheckBoxChecked; }
set
{
if (_IsCheckBoxChecked == value)
return;
_IsCheckBoxChecked = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("IsCheckBoxChecked"));
PropertyChanged(this, new PropertyChangedEventArgs("IsCheckBoxVisible"));
}
}
}
public bool IsCheckBoxVisible
{
get { return !_IsCheckBoxChecked; }
set
{
if (_IsCheckBoxVisible == value)
return;
_IsCheckBoxVisible = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("IsCheckBoxVisible"));
}
(构造器和INotifyPropertyChanged
实施不再赘述。)在MainPage.xaml中布置如下
控件。
<Window.Resources>
<local:VisibilityConverter x:Key="VisibilityConverter"/>
</Window.Resources>
<Window.DataContext>
<local:ItemViewModel/>
</Window.DataContext>
<Grid>
<StackPanel>
<CheckBox x:Name="checkBox" Content="Hide CheckBoxes" FontSize="14" IsChecked="{Binding IsCheckBoxChecked, Mode=TwoWay}" />
<ListView ItemsSource="{Binding Items}" HorizontalContentAlignment="Stretch" >
<ListView.ItemTemplate >
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding ItemName}"/>
<CheckBox Grid.Column="1" Visibility="{Binding IsCheckBoxVisible, Converter={StaticResource VisibilityConverter}}" >
<CheckBox.DataContext>
<local:ItemViewModel/>
</CheckBox.DataContext>
</CheckBox>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<StackPanel Orientation="Horizontal" Margin="4,4,0,0">
<TextBlock Text="IsCheckBoxVisible:"/>
<TextBlock Text="{Binding IsCheckBoxVisible}" Margin="4,0,0,0" FontWeight="Bold" />
</StackPanel >
<Button Content="Button" Visibility="{Binding IsCheckBoxVisible, Converter={StaticResource VisibilityConverter}}" Margin="4,4,4,4"/>
</StackPanel>
</Grid>
在“隐藏复选框”复选框被绑定到IsCheckBoxChecked
和用于更新IsCheckBoxVisible
。我还在DataTemplate
下面添加了一些额外的控件来证明(对我自己而言)一切正常。)
我也实现了Jeff Wilcox的值转换器。 (谢谢。)http://www.jeff.wilcox.name/2008/07/visibility-type-converter/
当我运行应用程序,检查和取消选中“隐藏复选框”,如预期的那样DataTemplate
功能外控,但是,唉,数据模板内Checkbox
保持不变。
我曾与成功:
IsVisible="{Binding IsChecked, Converter={StaticResource VisibilityConverter}, ElementName=checkBox}"
但我不只是试图模仿另一个控制,但要根据价值决定。
我会真正感谢您可以提供的任何帮助或建议。
谢谢。
在Visual Studio的调试输出窗口中是否有任何绑定错误?他们通常很好地指出发生了什么问题。 – ChrisF
克里斯。感谢您的回应。检查输出窗口,并且,如您所怀疑的那样,出现错误。它无法'找到'IsCheckBoxVisible。根据Duncan的回复,下面的应用修复,现在一切正常。谢谢。 – Dowse