我有一个控件,需要使用一个复选框来启用禁用部分,但我需要将它绑定到一个类所以我已经完成了以下操作。所以我的问题是我如何设置绑定到例如IncidentBuilderProperty.IsEnabled我应该使用参数进行代码编写。
<DockPanel DockPanel.Dock="Top" >
<Label Content="Display" />
<CheckBox Name="chkDisplayAdvanced" IsThreeState="False" VerticalAlignment="Center"
IsChecked="{Binding isEnabled, Mode=TwoWay}" />
</DockPanel>
但在我的模型中,我有以下方式声明属性isEnabled。
所以我的问题是如何访问属性IncidentBuilderProperty.isEnabled,记住它们是每个属性的屏幕,上面有此复选框,但引用了一个自定义控件。
public class AssignedToMeViewData : WizardData, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public ManagementPack ManagmentPack { get; set; }
public List<BuilderProperties> BuilderProperty { get; set; }
public BuilderProperties GeneralPage { get; set; }
public Exception LastKnownException { get; set; }
[DefaultValue(true)]
public bool IsNew { get; set; }
public BuilderProperties IncidentBuilderProperty { get; set; }
public BuilderProperties ProblemBuilderProperty { get; set; }
public BuilderProperties ServiceRequestBuilderProperty { get; set; }
public BuilderProperties ChangeRequestBuilderProperty { get; set; }
public BuilderProperties RleaseRequestBuilderProperty { get; set; }
public BuilderProperties ReviewActivityBuilderProperty { get; set; }
public BuilderProperties ManualActivityBuilderProperty { get; set; }
public class BuilderProperties
{
private bool isEnabled = true;
public bool IsEnabled {
get {
return isEnabled;
}
set {
if (isEnabled == value)
return;
isEnabled = value;
}
}
}
}
答案 0 :(得分:0)
在控件的AssignedToMeViewData
中设置DataContext
,您可以在xaml中设置与BuilderProperties
的所有绑定:
<DockPanel DockPanel.Dock="Top" >
<Label Content="Display" />
<CheckBox x:Name="chkIncident" IsChecked="{Binding IncidentBuilderProperty.IsEnabled, Mode=TwoWay}" VerticalAlignment="Center" />
<CheckBox x:Name="chkProblem" IsChecked="{Binding ProblemBuilderProperty.IsEnabled, Mode=TwoWay}" VerticalAlignment="Center" />
<CheckBox x:Name="chkService" IsChecked="{Binding ServiceRequestBuilderProperty.IsEnabled, Mode=TwoWay}" VerticalAlignment="Center" />
<CheckBox x:Name="chkChanges" IsChecked="{Binding ChangeRequestBuilderProperty.IsEnabled, Mode=TwoWay}" VerticalAlignment="Center" />
...
</DockPanel>
但请注意Mode=TwoWay
是CheckBox.Checked
的默认值,因此您无需指定。
如果必须从代码隐藏中执行此操作,则可以设置类似的绑定:
chkIncident.SetBinding(CheckBox.IsCheckedProperty,
new Binding("IncidentBuilderProperty.IsEnabled"));
chkProblem.SetBinding(CheckBox.IsCheckedProperty,
new Binding("ProblemBuilderProperty.IsEnabled"));
chkService.SetBinding(CheckBox.IsCheckedProperty,
new Binding("ServiceRequestBuilderProperty.IsEnabled"));
...
在从代码隐藏创建绑定时,请参阅kishore对您的问题的评论,以获取更多高级选项。
答案 1 :(得分:0)
请尝试以下代码段
FrameworkElement targetObject = chkDisplayAdvanced;
DependencyProperty targetProperty = CheckBox.IsCheckedProperty;
object sourceObject = IncidentBuilderProperty;//object of the actual Incidentbuilderclasss here
string sourceProperty = "IsEnabled";
var binding = new Binding(sourceProperty) {
Source = sourceObject, Mode = BindingMode.TwoWay
};
targetObject.SetBinding(targetProperty, binding);