Tag: wpf

使用WPF绑定传递两个命令参数

我有一个命令,我使用以下标准语法从我的XAML文件执行: <Button Content="Zoom" Command="{Binding MyViewModel.ZoomCommand}"/> 这工作得很好,直到我意识到我需要从视图中的两条信息,以使这个操作完成用户期望的方式(特定的canvas的宽度和高度)。 它似乎有可能将一个数组作为parameter passing给我的命令,但我没有看到有一种方法来指定绑定到我在CommandParameter中的两个canvas属性: <Button Content="Zoom" Command="{Binding MyViewModel.ZoomCommand" CommandParameter={Binding ElementName=MyCanvas, Path=Width}"/> 我如何通过宽度和高度我的命令? 这似乎不可能使用来自XAML的命令,我需要在代码隐藏中连接一个点击处理程序来获取这个信息传递给我的缩放方法。

如何以编程方式退出WPF应用程序?

在使用C#(WinForms)的几年中,我从来没有使用过WPF。 但是,现在我喜欢WPF,但是我不知道当用户从File菜单中单击Exit菜单项时我该如何退出我的应用程序。 我努力了: this.Dispose(); this.Exit(); Application.ShutDown(); Application.Exit(); Application.Dispose(); 等等。 没有任何工作

我的图像模糊! 为什么不是WPF的SnapsToDevicePixels工作?

我在WPF应用中使用了一些图片。 XAML: <Image Name="ImageOrderedList" Source="images/OrderedList.png" ToolTip="Ordered List" Margin="0,0,5,5" Width="20" Height="20" SnapsToDevicePixels="True" MouseUp="Image_MouseUp" MouseEnter="Image_MouseEnter" MouseLeave="Image_MouseLeave" /> 但是,他们似乎模糊: 死链接 – 模糊的WPF图像 这是一个放大,并排的比较。 原件在左边: 死链接 – 模糊的WPF图像放大 为什么SnapsToDevicePixels="True"行不能防止这个问题?

当DataTemplate是Button时,ListBox项返回string

我正在创build一个WP 8.1 Silverlight应用程序。 我有一个string名称的ObservableCollection ,它被设置为ListBox的ItemsSource 。 ListBox中button的名称。 然后我想从ListBox提取button,但返回值是stringtypes。 xaml代码是: <ListBox x:Name="Game_ScrollViewer_online" Margin="41,104,128,6" SelectedValuePath="Current_game_button"> <ListBox.ItemTemplate> <DataTemplate> <Button x:Name="Current_game_button" Content="{Binding}" HorizontalAlignment="Center" Height="80" Margin="-14,6,-15,0" VerticalAlignment="Top" Width="210" Template="{StaticResource Button_CurrentLayout1}" RenderTransformOrigin="0.5,0.5" Foreground="#FFCBECCB" FontFamily="Times New Roman" Click="LoadGame_online" FontSize="16"> </Button> </DataTemplate> </ListBox.ItemTemplate> </ListBox> 然后我想提取button元素: for (int i = 0; i < Game_ScrollViewer_online.Items.Count; i++) { var tempType = Game_ScrollViewer_online.Items[i].GetType(); Button tempBut = (Game_ScrollViewer_online.Items[i] […]

WPF ListView非活动select颜色

我正在创build一个WPF应用程序,其中有几个ListViewselect在一行(类似于iTunes浏览器)。 问题是默认的非活动select颜色太浅。 (见下文) 我怎样才能改变这种颜色,所以我不活动的listview看起来像这样? (见下文) 解 用像这样的Style覆盖默认的SystemColor: <Style TargetType="ListViewItem"> <Style.Resources> <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="{x:Static SystemColors.HighlightColor}"/> </Style.Resources> </Style>

WPF MVVM TreeView SelectedItem

这不可能是如此困难。 WPF中的TreeView不允许你设置SelectedItem,说属性是ReadOnly。 我有TreeView填充,甚至更新数据绑定收集更改时。 我只需要知道什么项目被选中。 我正在使用MVVM,所以没有代码隐藏或variables来引用树视图。 这是我find的唯一的解决scheme ,但它是一个明显的黑客攻击,它创build另一个元素在XAML中使用ElementName绑定将其自身设置为树视图select项目,然后您必须绑定您的Viewmodel。 有几个其他的问题被问到,但没有给出其他工作解决scheme。 我已经看到这个问题 ,但使用给出的答案给我编译错误,出于某种原因,我不能添加到我的项目混合sdk System.Windows.Interactivity的引用。 它说“未知的错误system.windows尚未预装”,我还没有想出如何过去的。 对于奖励积分:为什么微软会让这个元素的SelectedItem属性为ReadOnly?

WPF:如何使canvas自动resize?

我想我的Canvas自动调整其大小的项目,以便ScrollViewer查看器滚动条具有正确的范围。 这可以在XAML中完成吗? <ScrollViewer HorizontalScrollBarVisibility="Auto" x:Name="_scrollViewer"> <Grid x:Name ="_canvasGrid" Background="Yellow"> <Canvas x:Name="_canvas" HorizontalAlignment="Left" VerticalAlignment="Top" Background="Green"></Canvas> <Line IsHitTestVisible="False" …/> </Grid> </ScrollViewer> 在上面的代码中,canvas总是具有0的大小,尽pipe它不剪切它的子元素。

如何在wpf中使用RelayCommand?

如何在wpf中使用RelayCommand ?

我如何获得当前行号?

这是我想要做的一个例子: MessageBox.Show("Error line number "+CurrentLineNumber); 当前行号将是这段代码的源代码中的行号。 我怎样才能做到这一点?

第一次调用CanExecute时,WPF CommandParameter为NULL

我遇到了绑定到一个ItemsControl的DataTemplate中的Button的WPF和命令的问题。 情况非常简单。 ItemsControl绑定到一个对象列表,我希望能够通过单击button来删除列表中的每个对象。 该button执行一个命令,并且该命令负责删除。 CommandParameter绑定到我想要删除的对象。 这样我就知道用户点击了什么。 用户应该只能删除他们的“自己的”对象 – 所以我需要在命令的“CanExecute”调用中进行一些检查,以validation用户是否具有正确的权限。 问题是传递给CanExecute的参数在第一次被调用时是NULL,所以我不能运行逻辑来启用/禁用命令。 但是,如果我启用了allways,然后单击button执行该命令,CommandParameter正确传入。 这意味着对CommandParameter的绑定正在工作。 ItemsControl和DataTemplate的XAML如下所示: <ItemsControl x:Name="commentsList" ItemsSource="{Binding Path=SharedDataItemPM.Comments}" Width="Auto" Height="Auto"> <ItemsControl.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <Button Content="Delete" FontSize="10" Command="{Binding Path=DataContext.DeleteCommentCommand, ElementName=commentsList}" CommandParameter="{Binding}" /> </StackPanel> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> 所以你可以看到我有一个评论对象的列表。 我想DeleteCommentCommand的CommandParameter绑定到Command对象。 所以我想我的问题是:有没有人遇到过这个问题? CanExecute在我的Command上被调用,但是这个参数第一次总是NULL – 为什么? 更新:我能够把问题缩小一点。 我添加了一个空的Debug ValueConverter,以便在CommandParameter数据绑定时输出消息。 原来问题是CanExecute方法在CommandParameter绑定到button之前执行。 我试图设置CommandParameter之前的命令(如build议) – 但它仍然无法正常工作。 有关如何控制它的任何提示。 Update2:有没有什么办法来检测绑定是否“完成”,以便我可以强制重新评估命令? 另外 – 这是一个问题,我有多个button(ItemsControl中的每个项目)绑定到一个Command对象的同一个实例? Update3:我已经上传了一个bug的复制到我的SkyDrive: http […]