我写了这个枚举(标志):
using System;
namespace AndreyBushman.AutoCAD {
// Specifies, how and when .NET application will load.
// It is a flag.
[Flags]
public enum LoadCtrls {
// Load application upon detection of proxy object
AtProxyFound = 1,
// Load the application at startup
AtAutoCADStart = 2,
// Load the application at start of a command
AtCommandStart = 4,
// Load the application at the request of a user
// or another application
AtUserOrApplicationRequirement = 8,
// Do not load the application
NotLoad = 16,
// Load the application transparently
TransparencyLoad = 32,
}
}
现在我已经创建StackPanel
了CheckBox
项目:
<StackPanel x:Name="stackAllUsersLoadCtrls">
<CheckBox Margin="2">At proxy found</CheckBox>
<CheckBox Margin="2">At AutoCAD start</CheckBox>
<CheckBox Margin="2">At command start</CheckBox>
<CheckBox Margin="2">At user or application requirement</CheckBox>
<CheckBox Margin="2">Not load</CheckBox>
<CheckBox Margin="2">Transparency load</CheckBox>
</StackPanel>
结果画面:
如何将LoadCtrls
(它是一个标志)的实例与CheckBox
项目绑定?
UPD 我已经重写了我的代码(添加了包装器、重写了转换器和 XAML):
包装:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ab = AndreyBushman.AutoCAD;
using System.ComponentModel;
namespace AcadLoadManaged {
public sealed class LoadCtrlsWrapper : INotifyPropertyChanged {
ab.LoadCtrls loadCtrls;
public ab.LoadCtrls GetloadCtrls() {
return loadCtrls;
}
public LoadCtrlsWrapper() {
loadCtrls = default(ab.LoadCtrls);
}
public LoadCtrlsWrapper(ab.LoadCtrls loadCtrls) {
this.loadCtrls = loadCtrls;
}
public Boolean AtAutoCADStart {
get {
return (loadCtrls & ab.LoadCtrls.AtAutoCADStart) != 0;
}
set {
if (value)
loadCtrls |= ab.LoadCtrls.AtAutoCADStart;
else
loadCtrls &= ~ab.LoadCtrls.AtAutoCADStart;
OnPropertyChanged("AtAutoCADStart");
}
}
public Boolean AtCommandStart {
get {
return (loadCtrls & ab.LoadCtrls.AtCommandStart) != 0;
}
set {
if (value)
loadCtrls |= ab.LoadCtrls.AtCommandStart;
else
loadCtrls &= ~ab.LoadCtrls.AtCommandStart;
OnPropertyChanged("AtCommandStart");
}
}
public Boolean AtProxyFound {
get {
return (loadCtrls & ab.LoadCtrls.AtProxyFound) != 0;
}
set {
if (value)
loadCtrls |= ab.LoadCtrls.AtProxyFound;
else
loadCtrls &= ~ab.LoadCtrls.AtProxyFound;
OnPropertyChanged("AtProxyFound");
}
}
public Boolean AtUserOrApplicationRequirement {
get {
return (loadCtrls & ab.LoadCtrls.AtUserOrApplicationRequirement) != 0;
}
set {
if (value)
loadCtrls |= ab.LoadCtrls.AtUserOrApplicationRequirement;
else
loadCtrls &= ~ab.LoadCtrls.AtUserOrApplicationRequirement;
OnPropertyChanged("AtUserOrApplicationRequirement");
}
}
public Boolean NotLoad {
get {
return (loadCtrls & ab.LoadCtrls.NotLoad) != 0;
}
set {
if (value)
loadCtrls |= ab.LoadCtrls.NotLoad;
else
loadCtrls &= ~ab.LoadCtrls.NotLoad;
OnPropertyChanged("NotLoad");
}
}
public Boolean TransparencyLoad {
get {
return (loadCtrls & ab.LoadCtrls.TransparencyLoad) != 0;
}
set {
if (value)
loadCtrls |= ab.LoadCtrls.TransparencyLoad;
else
loadCtrls &= ~ab.LoadCtrls.TransparencyLoad;
OnPropertyChanged("TransparencyLoad");
}
}
void OnPropertyChanged(String propertyName) {
PropertyChangedEventHandler temp = PropertyChanged;
if (null != temp)
temp(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
}
}
转换器:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using ab = AndreyBushman.AutoCAD;
namespace AcadLoadManaged {
public sealed class LoadCtrlsConverter : IValueConverter {
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture) {
ab.LoadCtrls x = (ab.LoadCtrls)value;
return new LoadCtrlsWrapper(x);
}
public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture) {
LoadCtrlsWrapper x = (LoadCtrlsWrapper)value;
return x.GetloadCtrls();
}
}
}
XAML:
<GroupBox Grid.Row="4" Header="Load this plugin, when...:" Grid.Column="0"
Grid.ColumnSpan="2" Padding="3">
<GroupBox.DataContext>
<Binding ElementName="lstAllUsers" Path="SelectedItem.LoadCtrls" Mode="TwoWay">
<Binding.Converter>
<local:LoadCtrlsConverter/>
</Binding.Converter>
</Binding>
</GroupBox.DataContext>
<StackPanel>
<CheckBox Margin="2" Content="At proxy found" IsChecked="{Binding Path=AtProxyFound}"/>
<CheckBox Margin="2" Content="At AutoCAD start" IsChecked="{Binding Path=AtAutoCADStart}"/>
<CheckBox Margin="2" Content="At command start" IsChecked="{Binding Path=AtCommandStart}"/>
<CheckBox Margin="2" Content="At user or application requirement"
IsChecked="{Binding Path=AtUserOrApplicationRequirement}"/>
<CheckBox Margin="2" Content="Not load" IsChecked="{Binding Path=NotLoad}"/>
<CheckBox Margin="2" Content="Transparency load" IsChecked="{Binding Path=TransparencyLoad}"/>
</StackPanel>
</GroupBox>
但我的绑定只有一种方式。如果我修改CheckBox
值 - 它们不会保存。我已经在ConvertBack
方法上尝试过断点,但它没有发生。为什么?