我有一个奇怪的绑定行为DataContext
& IsEnabled
UserControl的属性。奇怪的行为在UserControl上绑定DataContext&IsEnabled-Property
在我的网页,我用这样的用户控件:
<httpsPort:HttpsPort DataContext="{Binding Path=Https}"
IsEnabled="{Binding CurrentServiceState, Converter={StaticResource ServiceStateIsConfigableConverter}}" />
和一个按钮像这样:
<Button Content="start service"
IsEnabled="{Binding CurrentServiceState, Converter={StaticResource ServiceStateIsConfigableConverter}}"
Command="{Binding CmdConfigureService}" [...] />
解释:
转换器的currentServiceState-枚举到转换布尔。我的按钮的行为如我所料(En/DisAbled)。
问题:我的按钮是正确的en /禁用,但我的用户控件中的控件不是。
DataContext的(HTTPS)实际上是不为空:
private HttpsPortViewModel _https;
public HttpsPortViewModel Https
{
get
{
if (_https == null)
{
_https = new HttpsPortViewModel();
}
return _https;
}
set
{
_https = value;
NotifyPropertyChanged(() => Https);
}
}
我曾尝试使用FallbackValue =我的用户控件绑定假,但随后用户控件甚至禁用...
任何人可以解释这些行为? 非常感谢。
更新:
我的解决方法:
<Grid IsEnabled="{Binding CurrentServiceState, Converter={StaticResource ServiceStateIsConfigableConverter}}">
<httpsPort:HttpsPort DataContext="{Binding Path=Https}" />
</Grid>
你从来没有真正说过你的问题。请这样做。 – 2012-04-16 09:05:41
嗨丹尼尔,问题是,用户控件不是像按钮一样被禁用/禁用。 – 2012-04-16 09:17:32
我不认为绑定DataContext属性是个好主意。你为什么要为usercontrol做这件事?它似乎适用于没有它的按钮。 – 2012-04-16 09:28:52