私はXAMLの新機能ですが、学習するのが楽しいです。私が本当に苦労しているのは、要素にプロパティをバインドすることです。DataTemplate
です。XAML:DataTemplateのプロパティをバインドする
単純なWPFの例を作成しましたが、うまくいけば私の問題を説明できます。
私はVisibility
のCheckBox
のプロパティをにバインドしようとしていますが、私のviewmodelのプロパティにバインドしようとしています。
私はItem
という単純なデータモデルを持っていますが、この例ではほとんど関係ありません。
class Item : INotifyPropertyChanged
{
// Fields...
private bool _IsRequired;
private string _ItemName;
そして、非常に単純なViewViewという名前のモデルViewViewModel。
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のバリューコンバータも実装しました。 、私は「隠すチェックボックス」をチェックし、チェックをはずす、アプリを実行すると、予想通り、DataTemplate
関数の外部制御http://www.jeff.wilcox.name/2008/07/visibility-type-converter/
を(ありがとう)悲しいかな、データテンプレート内のCheckbox
は変わりません。
私は成功を収めている:
IsVisible="{Binding IsChecked, Converter={StaticResource VisibilityConverter}, ElementName=checkBox}"
しかし、私はちょうど別のコントロールを模倣するが値に基づいて決定を下すつもりはありませんよ。
あなたが提供できるヘルプやアドバイスは本当にありがたいです。
ありがとうございます。
Visual Studioのデバッグ出力ウィンドウにバインディングエラーがありますか?彼らは通常何がうまくいかないのかを示す良い指標です。 – ChrisF
Chris。ご回答いただきありがとうございます。アウトプットウィンドウをチェックし、疑わしい通りにエラーが発生しました。 IsCheckBoxVisibleが見つかりませんでした。以下のダンカンの返答のように修正を適用してください。ありがとうございました。 – Dowse