我对这个bug很疯狂,所以我真的需要帮助。
在捕获异常时,我应该显示一个包含网格的“消息框”(使用网格),但是在将数据上下文显式设置为网格之后,它仍然没有显示消息框
XAML代码:
<Grid x:Name="gridBackground" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="1" Visibility="{Binding BackgroundVisibility}" Background="#FFB4B4B4" Opacity="0.7"></Grid>
<Grid x:Name="gridWarningMessages" HorizontalAlignment="Center" VerticalAlignment="Center" MinWidth="500" MinHeight="200" MaxWidth="600" MaxHeight="600" Visibility="{Binding WarningMessagesVisibility}">
<TextBlock HorizontalAlignment="Center" TextWrapping="Wrap" VerticalAlignment="Center" Foreground="Black" Margin="50" FontSize="29.333" TextAlignment="Center" x:Name="txtWarningMessages" Text="{Binding WarningOrErrorMessage}" FontFamily="Assets/helvetica-neue-thin-1361522098.ttf#HelveticaNeue"></TextBlock>
</Grid>
ViewModel属性:
private string warningOrErrorMessage;
public string WarningOrErrorMessage
{
get { return warningOrErrorMessage; }
set
{
if (warningOrErrorMessage != value)
{
warningOrErrorMessage = value;
Notify("WarningOrErrorMessage");
}
}
}
private Visibility warningMessagesVisibility;
public Visibility WarningMessagesVisibility
{
get { return warningMessagesVisibility; }
set
{
if (warningMessagesVisibility != value)
{
warningMessagesVisibility = value;
Notify("WarningMessagesVisibility");
}
}
}
private Visibility backgroundVisibility;
public Visibility BackgroundVisibility
{
get { return backgroundVisibility; }
set
{
if (backgroundVisibility != value)
{
backgroundVisibility = value;
Notify("BackgroundVisibility");
}
}
}
将datacontext显式设置为网格的代码,并在初始化页面时“隐藏”它们:
var context = (SubmittedOrdersViewModel)DataContext;
gridWarningMessages.DataContext = context;
gridBackground.DataContext = context;
context.BackgroundVisibility = Visibility.Collapsed;
context.WarningMessagesVisibility = Visibility.Collapsed;
在捕获到异常后应该显示网格的代码(在viewmodel中,通过ICommand ..)(请注意注释'Notify'行,我已尝试在属性上设置值后显式触发Notify但仍有同样的问题):
private void deleteSelectedOrder(object param)
{
try
{
throw new Exception("");
//working code to delete the order goes here
}
catch (Exception)
{
WarningOrErrorMessage = "An error occurred while trying to delete this order.";
Notify("WarningOrErrorMessage");
BackgroundVisibility = Visibility.Visible;
//Notify("BackgroundVisibility");
WarningMessagesVisibility = Visibility.Visible;
//Notify("WarningMessagesVisibility");
}
}
拜托,请帮助我。 :(谢谢你!
答案 0 :(得分:0)
我刚刚发现有一段代码在页面初始化时直接在控件的属性上显式设置visibility属性:
gridWarningMessages.Visibility = Visibility.Collapsed;
gridBackground.Visibility = Visibility.Collapsed;
所以我删除了那段代码,取而代之的是更改了viewmodel-bound属性:
context.BackgroundVisibility = Visibility.Collapsed;
context.WarningMessagesVisibility = Visibility.Collapsed;
现在正在运作。
随时评论,WPF专家。您是否遇到过这样的场景,即在控件的属性上显式设置值而不是viewmodel属性会混淆代码?
非常感谢那些花时间查看我的代码的人。