我的问题是图像加载似乎与应用程序资源不正确.这是代码:
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.UriSource = new Uri(@"pack://application:,,,/WpfApplication3;component/Resources/Images/16x16_incorrect.png", UriKind.Absolute);
bi.EndInit();
ImageSource s = bi;
Run Code Online (Sandbox Code Playgroud)
图像文件"16x16_incorrect.png"是16x16 32bpp PNG文件,但在执行上面的代码后,s.Width = s.Height = 21,59729 ....我还有另一个文件 - "16x16_correct.png",当我加载它以相同的方式,ImageSource的宽度和高度都等于16,002.
我有一大堆有用的PNG 16x16 32bpp图像,我打算在我的应用程序的UI中使用它.不幸的是,它们每个都加载不正确并且看起来模糊(或平滑),因为系统将它从16x16拉伸到21x21.
您是否愿意如此友好地解释这个问题的可能解决方案?如果源图像文件中存在问题,如何将ImageSource.Width更改为所需大小才能使用此文件?
Button.CommandProperty绑定到ViewModel的SomeObject.SomeCommand属性.当SomeObject的SomeCommand属性设置为null或整个SomeObject属性设置为null时,此按钮保持启用状态.在这种情况下如何禁用按钮?
我正在使用MVVM创建应用程序,其行为类似于浏览器:主view_model(对应于主窗口作为视图)具有Workspace view_models列表.每个工作区view_model对应于Windows的TabControl中的TabPage.主view_model具有CurrentWorkspace属性,该属性对应当前活动的TabPage.
在主窗口中,带有按钮的工具栏,其中使用了CurrentWorkspace提供的命令.例如,对重新加载工作空间数据的访问实现为:
<Button Name="btReload" Content="Reload"
Command="{Binding Path=CurrentWorkspace.ReloadCommand, UpdateSourceTrigger=PropertyChanged}"/>
Run Code Online (Sandbox Code Playgroud)
我试图通过创建DataTriggers来完成按钮禁用的任务,但似乎触发器仅在第一次工作而不再工作:
<Button Name="btReload" Content="Reload"
Command="{Binding Path=CurrentWorkspace.ReloadCommand, UpdateSourceTrigger=PropertyChanged}">
<Button.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=CurrentWorkspace, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" Value="{x:Null}">
<Setter Property="dxb:BarButtonItem.IsEnabled" Value="False"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=CurrentPage.CurrentWorkspace, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" Value="{x:Null}">
<Setter Property="dxb:BarButtonItem.IsEnabled" Value="False"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
Run Code Online (Sandbox Code Playgroud)
它看起来真的很愚蠢:就像MS Word一样,无文档客户区,同时工具栏中有很多现成的按钮(带格式化和其他特定于文档的功能).请帮我, :)
PS向工具栏添加带有绑定到CurrentWorkspace的DataContext的按钮时,在窗口中激活或添加或删除工作区选项卡时,其DataContextChanged事件会正常触发.所以,问题出在DataTrigger中(或者通常是View中),而不是它的ViewModel(s).
我在VS2010上传了示例项目,链接存档:http://www.filefactory.com/file/b43455e/n/WhatIfCommandIsNull.rar
如下所述.
如您所见,当Window.DataContext设置为ViewModel实例时,两个命令都正常工作,&ResetDataCommand.CanExecute也正常工作(当ViewModel.Data为NULL时,ResetDataCommand.CanExecute返回false并且按钮btResetData被禁用).一旦Window.DataContext设置为null,最后两个按钮启用(对于前两个按钮,没有命令被绑定).
问题是声明性地(通过触发器)实现接下来的四个规则:
我认为使用触发器可以实现前两个规则,其次是使用DataTriggers.但他们没有工作,所以我从项目中删除了它们.
我需要在"父"对象中检查是否可以在某个时刻接受在"子"中调用某些方法.例如,父对象(组件)包括子对象(或者换句话说是组件),而父对象现在正在处理,因此必须禁止所有(或特定的)子活动(即启动新的服务线程,将新的客户端请求排入队列,... ).
public class Parent
{
public bool IsMethodCallAcceptable(reference_to_method) {...}
}
public class Child
{
public int SomeMethod(int intArg, string stringArg)
{
if(!_parent.IsMethodCallAcceptable(reference_to_SomeMethod_with_actual_args))
throw new ...
...
}
private void AnotherMethod(string param = null) {...}
{
if(!_parent.IsMethodCallAcceptable(reference_to_AnotherMethod_with_actual_args))
throw new ...
...
}
private Guid ThirdMethod()
{
if(!_parent.IsMethodCallAcceptable(reference_to_ThirdMethod))
throw new ...
...
}
}
Run Code Online (Sandbox Code Playgroud)
有什么办法吗?