C# 禁用和启用代码隐藏后,WPF复选框检查不工作
我有ComboBox,并基于复选框启用它。如果我在代码隐藏中禁用并启用复选框,那么它将停止工作。但是,如果我不这样做,那么当复选框被选中时,组合框将被启用,而当复选框被取消选中时,组合框将被禁用 代码示例如下:C# 禁用和启用代码隐藏后,WPF复选框检查不工作,c#,wpf,C#,Wpf,我有ComboBox,并基于复选框启用它。如果我在代码隐藏中禁用并启用复选框,那么它将停止工作。但是,如果我不这样做,那么当复选框被选中时,组合框将被启用,而当复选框被取消选中时,组合框将被禁用 代码示例如下: <CheckBox x:Name="ckBox1" IsChecked="{Binding proceed, TargetNullValue=false}" Content="Proceed" FontSize="16"/> 好的,这不是一个很好的解决方案,但它应该让你做你
<CheckBox x:Name="ckBox1" IsChecked="{Binding proceed, TargetNullValue=false}" Content="Proceed" FontSize="16"/>
好的,这不是一个很好的解决方案,但它应该让你做你需要做的。基本上,在将IsEnabled设置为false之前,需要获取对现有绑定对象的引用。然后在将IsEnabled设置为false或返回true后,重置绑定。我在加载的事件处理程序中设置的完整代码,但只需移动到需要的地方
this.Loaded += (s, e) =>
{
var controls = this.sourceGrid.Children.OfType<Control>();
controls.ToList().ForEach(c =>
{
ComboBox combo = c as ComboBox;
Binding newBinding = null;
if (combo != null)
{
newBinding = BindingOperations.GetBinding(combo, ComboBox.IsEnabledProperty);
}
c.IsEnabled = false;
if (combo != null)
{
BindingOperations.SetBinding(combo, ComboBox.IsEnabledProperty, newBinding);
}
});
controls.ToList().ForEach(c =>
{
ComboBox combo = c as ComboBox;
Binding newBinding = null;
if (combo != null)
{
newBinding = BindingOperations.GetBinding(combo, ComboBox.IsEnabledProperty);
}
c.IsEnabled = true;
if (combo != null)
{
BindingOperations.SetBinding(combo, ComboBox.IsEnabledProperty, newBinding);
}
});
};
this.Loaded+=(s,e)=>
{
var controls=this.sourceGrid.Children.OfType();
controls.ToList().ForEach(c=>
{
ComboBox combo=c作为ComboBox;
绑定newBinding=null;
如果(组合!=null)
{
newBinding=BindingOperations.GetBinding(combo,ComboBox.IsEnabledProperty);
}
c、 IsEnabled=false;
如果(组合!=null)
{
BindingOperations.SetBinding(combo,ComboBox.IsEnabledProperty,newBinding);
}
});
controls.ToList().ForEach(c=>
{
ComboBox combo=c作为ComboBox;
绑定newBinding=null;
如果(组合!=null)
{
newBinding=BindingOperations.GetBinding(combo,ComboBox.IsEnabledProperty);
}
c、 IsEnabled=true;
如果(组合!=null)
{
BindingOperations.SetBinding(combo,ComboBox.IsEnabledProperty,newBinding);
}
});
};
我必须强调的是,这段代码并不漂亮,所以请根据您自己的实现进行适当的重构。好的,这不是一个漂亮的解决方案,但它应该让您做您需要做的事情。基本上,在将IsEnabled设置为false之前,需要获取对现有绑定对象的引用。然后在将IsEnabled设置为false或返回true后,重置绑定。我在加载的事件处理程序中设置的完整代码,但只需移动到需要的地方
this.Loaded += (s, e) =>
{
var controls = this.sourceGrid.Children.OfType<Control>();
controls.ToList().ForEach(c =>
{
ComboBox combo = c as ComboBox;
Binding newBinding = null;
if (combo != null)
{
newBinding = BindingOperations.GetBinding(combo, ComboBox.IsEnabledProperty);
}
c.IsEnabled = false;
if (combo != null)
{
BindingOperations.SetBinding(combo, ComboBox.IsEnabledProperty, newBinding);
}
});
controls.ToList().ForEach(c =>
{
ComboBox combo = c as ComboBox;
Binding newBinding = null;
if (combo != null)
{
newBinding = BindingOperations.GetBinding(combo, ComboBox.IsEnabledProperty);
}
c.IsEnabled = true;
if (combo != null)
{
BindingOperations.SetBinding(combo, ComboBox.IsEnabledProperty, newBinding);
}
});
};
this.Loaded+=(s,e)=>
{
var controls=this.sourceGrid.Children.OfType();
controls.ToList().ForEach(c=>
{
ComboBox combo=c作为ComboBox;
绑定newBinding=null;
如果(组合!=null)
{
newBinding=BindingOperations.GetBinding(combo,ComboBox.IsEnabledProperty);
}
c、 IsEnabled=false;
如果(组合!=null)
{
BindingOperations.SetBinding(combo,ComboBox.IsEnabledProperty,newBinding);
}
});
controls.ToList().ForEach(c=>
{
ComboBox combo=c作为ComboBox;
绑定newBinding=null;
如果(组合!=null)
{
newBinding=BindingOperations.GetBinding(combo,ComboBox.IsEnabledProperty);
}
c、 IsEnabled=true;
如果(组合!=null)
{
BindingOperations.SetBinding(combo,ComboBox.IsEnabledProperty,newBinding);
}
});
};
我必须强调,这段代码并不漂亮,因此请根据您自己的实现进行适当的重构。您不需要任何代码来禁用和启用控件。用XAML来做
<Checkbox x:Name="enablingCheckBox" Content="Enabled?" IsChecked="{Binding IsEnabled}"/>
<Grid IsEnabled="{Binding IsEnabled, Mode=OneWay, ElementName=enablingCheckBox}">
<Button Content="Sample"/>
<ComboBox/>
</Grid>
将网格中的
IsEnabled
属性设置为false
将自动禁用属于该网格的所有控件。您不需要任何代码来禁用和启用控件。用XAML来做
<Checkbox x:Name="enablingCheckBox" Content="Enabled?" IsChecked="{Binding IsEnabled}"/>
<Grid IsEnabled="{Binding IsEnabled, Mode=OneWay, ElementName=enablingCheckBox}">
<Button Content="Sample"/>
<ComboBox/>
</Grid>
将网格中的
IsEnabled
属性设置为false
会自动禁用属于该网格的所有控件。您是否尝试过Mode=one-way
?您能否向我们显示隐藏的代码?您在隐藏的代码中更改了什么?属性“继续”或ckBox1.Enabled属性?@MikeEason,Mode=OneWay
不起作用。@Shamshiel,我正在更改网格中的所有控件,如您所见。我添加了代码隐藏。您是否尝试过Mode=one-way
?您能否向我们展示代码隐藏?您在代码隐藏中更改了什么?属性“继续”或ckBox1.Enabled属性?@MikeEason,Mode=OneWay
不起作用。@Shamshiel,我正在更改网格中的所有控件,如您所见。我添加了代码。问题是我不需要禁用整个网格,只需要禁用某些控件。然后在这些控件的IsEnabled
属性上设置绑定。问题是我不需要禁用整个网格,只需要禁用某些控件。然后在这些控件的IsEnabled
属性上设置绑定。
<Checkbox x:Name="enablingCheckBox" Content="Enabled?" IsChecked="{Binding IsEnabled}"/>
<Grid IsEnabled="{Binding IsEnabled, Mode=OneWay, ElementName=enablingCheckBox}">
<Button Content="Sample"/>
<ComboBox/>
</Grid>