我正在尝试构建一个非常简单的MVVM - 应用程序:
XAML:
<StackPanel>
<StackPanel.Resources>
<ad:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter"/>
</StackPanel.Resources>
<ad:DockingManager DocumentsSource="{Binding Path=Networks, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
ActiveContent="{Binding Path=Active, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Height="300">
<ad:LayoutRoot>
<ad:LayoutPanel>
<ad:LayoutDocumentPane/>
</ad:LayoutPanel>
</ad:LayoutRoot>
<ad:DockingManager.LayoutItemTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</ad:DockingManager.LayoutItemTemplate>
<ad:DockingManager.LayoutItemContainerStyle>
<Style TargetType="{x:Type ad:LayoutItem}">
<Setter Property="Title" Value="{Binding Model.Title}"/>
<Setter Property="Visibility" Value="{Binding Model.IsVisible, Mode=TwoWay, Converter={StaticResource BoolToVisibilityConverter}, ConverterParameter={x:Static Visibility.Hidden}}"/>
</Style>
</ad:DockingManager.LayoutItemContainerStyle>
</ad:DockingManager>
<ListBox ItemsSource="{Binding Path=Networks, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
SelectedItem="{Binding Path=Active, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Path=IsVisible, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Content="{Binding Path=Title, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
对于这个例子,我简单地说了一下。
这是我的ViewModel和我的模型(我知道不完全正确,但我想保持简短。)
public partial class MainWindow : Window, INotifyPropertyChanged
{
public MainWindow()
{
DataContext = this;
InitializeComponent();
Networks.Add(new DocumentVM("Title", "Text"));
Networks.Add(new DocumentVM("Title2", "Text2"));
Networks.Add(new DocumentVM("Title3", "Text3"));
}
private const string NetworksPropertyName = "Networks";
private ObservableCollection<DocumentVM> _Networks = new ObservableCollection<DocumentVM>();
public ObservableCollection<DocumentVM> Networks
{
get
{
return _Networks;
}
set
{
_Networks = value;
RaisePropertyChanged(NetworksPropertyName);
}
}
private const string ActivePropertyName = "Active";
private DocumentVM _Active;
public DocumentVM Active
{
get
{
return _Active;
}
set
{
_Active = value;
RaisePropertyChanged(ActivePropertyName);
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
模型
public class DocumentVM : INotifyPropertyChanged
{
public DocumentVM(string Title, string Text)
{
this.Title = Title;
this.Text = Text;
}
private const string IsVisiblePropertyName = "IsVisible";
private bool _IsVisible;
public bool IsVisible
{
get
{
return _IsVisible;
}
set
{
_IsVisible = value;
RaisePropertyChanged(IsVisiblePropertyName);
}
}
private const string TitlePropertyName = "Title";
private string _Title;
public string Title
{
get
{
return _Title;
}
set
{
_Title = value;
RaisePropertyChanged(TitlePropertyName);
}
}
private const string TextPropertyName = "Text";
private string _Text;
public string Text
{
get
{
return _Text;
}
set
{
_Text = value;
RaisePropertyChanged(TextPropertyName);
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
我想将文档设置为可见或通过复选框隐藏它们,但IsVisible属性不会更新。
谢谢!
编辑:
我发现如果我使用:
<ad:DockingManager DocumentsSource="{Binding Path=Networks, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
AnchorablesSource="{Binding Path=Networks, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
ActiveContent="{Binding Path=Active, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Height="300">
AnchorablesItems的隐藏工作。 我有可能无法隐藏文件吗? :(