我在这做错了什么?:
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button>
<Button.ToolTip>
<TextBlock Text="{Binding Path=Title, RelativeSource={RelativeSource AncestorType=Window}}" />
Run Code Online (Sandbox Code Playgroud)
这只是一个简单的例子,无论如何都不起作用:)实际上我需要从Window的DataContext范围内的另一个属性中获取一个值.
帮帮我吧
我想创建一个在后台执行操作的应用程序,并且可以通过Tray图标进行控制.此托盘图标有一个上下文菜单,其复选框可以设置为"已启用",然后启动后台任务.
我正在使用WPF和Hardcodet WPF NotifyIcon.
这是我的MainWindow.xaml:
<Window x:Name="mainWindow" x:Class="MyTrayApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MyTrayApplication"
xmlns:tb="http://www.hardcodet.net/taskbar"
mc:Ignorable="d"
Title="MainWindow" Height="10" Width="10" Visibility="Hidden">
<tb:TaskbarIcon x:Name="taskbarIcon" IconSource="MyTrayApplication.ico" ToolTipText="My tray application" >
<tb:TaskbarIcon.ContextMenu>
<ContextMenu>
<MenuItem x:Name="enabledItem" Header="Enabled" IsCheckable="True" IsChecked="{Binding Path=Enabled, ElementName=mainWindow, UpdateSourceTrigger=PropertyChanged}"/>
<MenuItem x:Name="configureItem" Header="Configure..." Click="configureItem_Click"/>
<MenuItem x:Name="exitItem" Header="Exit" Click="exitItem_Click"/>
</ContextMenu>
</tb:TaskbarIcon.ContextMenu>
</tb:TaskbarIcon>
</Window>
Run Code Online (Sandbox Code Playgroud)
这是我的代码背后:
using System;
using System.Windows;
namespace MyTrayApplication {
/// <summary>
/// Interaktionslogik für MainWindow.xaml
/// </summary>
public partial class MainWindow : Window {
private bool _enabled;
public MainWindow() {
InitializeComponent();
}
private …Run Code Online (Sandbox Code Playgroud) 我用对象填充了ListView,并将ContextMenu绑定到ListView中的那些项目.只能通过单击项目来打开ContextMenu.问题是Caliburn Micro抛出一个错误,它无法找到ShowProperties()的目标方法.
我认为出现此问题是因为Caliburn没有可用的ViewModel的正确DataContext.我在Stackoverflow上尝试了很多解决方案,使ViewModel可用于ContextMenu项目,但无济于事,例如:
Caliburn Message.Attach()抛出"找不到方法的目标"
这是我的观点的XAML代码:
<Window x:Class="CueMaster.Views.AppView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dragDrop="clr-namespace:GongSolutions.Wpf.DragDrop;assembly=GongSolutions.Wpf.DragDrop"
xmlns:cal="http://www.caliburnproject.org"
Height="500" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<ListView Grid.Column="1" Margin="5"
ItemsSource="{Binding Cues}"
dragDrop:DragDrop.IsDragSource="True"
dragDrop:DragDrop.IsDropTarget="True"
dragDrop:DragDrop.DropHandler="{Binding}">
<ListView.Resources>
<ContextMenu x:Key="ItemContextMenu">
<MenuItem Header="Properties" cal:Message.Attach="ShowProperties($dataContext)" Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListView}}">
<MenuItem.Icon>
<Image Source="../PropertyIcon.png" />
</MenuItem.Icon>
</MenuItem>
</ContextMenu>
</ListView.Resources>
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}" >
<Setter Property="ContextMenu" Value="{StaticResource ItemContextMenu}" />
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView >
<GridViewColumn Width="70" Header="Cue" DisplayMemberBinding="{Binding Id}" />
<GridViewColumn Width="100" …Run Code Online (Sandbox Code Playgroud) 任何 wpf 程序员迟早都会开始使用BindingProxy。
我试图通过将一些资源移动到单独的资源字典中来拆分 xaml。我的问题是资源包含对BindingProxy.
我该如何处理这种情况?
举个例子,假设有一个资源在BindingProxy某处使用
<Window.Resources>
<local:BindingProxy x:Key="proxy" />
<ControlTemplate x:Key="test">
<TextBlock Text="{Binding DataContext.Test, Source={StaticResource proxy}}" />
</ControlTemplate>
</Window.Resources>
<Control Template="{StaticResource test}" />
Run Code Online (Sandbox Code Playgroud)
和后面的代码
public partial class MainWindow : Window
{
public string Test { get; set; } = "Test 123";
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
}
Run Code Online (Sandbox Code Playgroud)
它可能不是最好的例子,使用BindingProxy并不合理,但它很好地达到了演示目的。在运行时窗口中将"Test 123"显示文本。
现在让我们尝试将资源移动到资源字典中Dictionary1.xaml
<ResourceDictionary ... >
<ControlTemplate x:Key="test">
<TextBlock Text="{Binding Test, Source={StaticResource proxy}}" /> <!-- error here …Run Code Online (Sandbox Code Playgroud)