我将重点放在Popup的开头:
wcl:FocusHelper.IsFocused="{Binding RelativeSource={RelativeSource Self}, Path=IsOpen}"
Run Code Online (Sandbox Code Playgroud)
FocusHelper类代码:
public static class FocusHelper
{
public static readonly DependencyProperty IsFocusedProperty =
DependencyProperty.RegisterAttached("IsFocused", typeof(bool?), typeof(FocusHelper), new FrameworkPropertyMetadata(IsFocusedChanged));
public static bool? GetIsFocused(DependencyObject element)
{
if (element == null)
{
throw new ArgumentNullException("element");
}
return (bool?)element.GetValue(IsFocusedProperty);
}
public static void SetIsFocused(DependencyObject element, bool? value)
{
if (element == null)
throw new ArgumentNullException("element");
element.SetValue(IsFocusedProperty, value);
}
private static void IsFocusedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var fe = (FrameworkElement)d;
if (e.OldValue == null)
{
fe.GotFocus += ElementGotFocus;
fe.LostFocus += …Run Code Online (Sandbox Code Playgroud) 我有Canvas一个TextBlock像这样:
<Canvas x:Name="ContentPanel" Grid.Row="1" DoubleTapped="ContentPanel_DoubleTapped">
<TextBlock x:Name="WordBlock" FontSize="226.667" FontFamily="Segoe UI Semilight" TextAlignment="Center"
RenderTransformOrigin="0.5, 0.5">
<TextBlock.RenderTransform>
<TranslateTransform x:Name="translate"/>
</TextBlock.RenderTransform>
</TextBlock>
</Canvas>
Run Code Online (Sandbox Code Playgroud)
我的应用程序是这样的,当用户导航到此页面时,TextBlock将以中心为中心Canvas,如果TextBlock宽度大于画布的宽度,则会出现选框动画:
private void SetAnimation()
{
Canvas.SetLeft(WordBlock, (ContentPanel.ActualWidth - WordBlock.ActualWidth) / 2);
Canvas.SetTop(WordBlock, (ContentPanel.ActualHeight - WordBlock.ActualHeight) / 2);
if (WordBlock.ActualWidth > ContentPanel.ActualWidth)
{
MarqueeAnimation.From = WordBlock.ActualWidth;
MarqueeAnimation.To = -WordBlock.ActualWidth;
MarqueeAnimation.Duration = new Duration(new TimeSpan(0, 0, 10));
MarqueeBoard.Begin();
}
}
Run Code Online (Sandbox Code Playgroud)
此方法称为OnNavigatedTo.我无法弄清楚为什么TextBlock不会居中,因为ActualHeight和ActualWidth属性总是以0.0的形式返回.我不想放置固定大小,因为这是一个Windows应用商店应用程序,并希望它可以在不同的屏幕尺寸上进行扩展.
有任何想法吗?我被卡住了.
我有一个大的WPF应用程序,我在用户控件中有一个datagrid,我需要为OnCreateAutomationPeer创建一个覆盖.我在做这件事时遇到了麻烦,事件似乎永远不会发生.在我的代码隐藏中,我有类似的东西
public partial class DocChecklistView : UserControl, IDataModuleView {
protected override System.Windows.Automation.Peers.AutomationPeer OnCreateAutomationPeer()
{
return null;
}
public CDocumentChecklistView() {
InitializeComponent();
}
}
Run Code Online (Sandbox Code Playgroud)
XAML非常标准,代码类似
<UserControl>
<Grid>
<toolkit:DataGrid ItemsSource="{Binding Source={StaticResource DocumentsVS}}" AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="False"
FontSize="16" Name="_dgDocuments" Style="{StaticResource EklektosDataGridStyle}" . . . >
</UserControl>
Run Code Online (Sandbox Code Playgroud)
在上面,将toolkit:DataGrid设置为WPFToolkit的命名空间.DataGrid设计的作品,我从来没有在用户控件中完成覆盖,上面的代码永远不会触发 - 从未到达断点.
有什么想法吗?
我有一个UserControl,我想在我的MainWindow上加载多次.为此,我使用ItemsControl:
<ItemsControl Grid.Row="1"
ItemsSource="{Binding FtpControlList, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal"
IsItemsHost="True" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type my:BackUpControl}">
<my:BackUpControl Margin="5"
Width="500" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)
我的UserControl由ViewModel绑定.我的MainWindow也有一个ViewModel.在MainWindowViewModel中,我有一个OberservableCollection依赖项属性,该属性需要一个UserControlViewModel列表.在MainWindowViewModel的构造函数中,我将一些UserControlViewModels添加到List中.
public MainWindowViewModel()
{
FtpControlList = new ObservableCollection<BackUpControlViewModel>();
FtpControlList.Add(new BackUpControlViewModel("View 1"));
FtpControlList.Add(new BackUpControlViewModel("View 2"));
FtpControlList.Add(new BackUpControlViewModel("View 3"));
}
public static readonly DependencyProperty FtpControlListProperty = DependencyProperty.Register("FtpControlList", typeof(ObservableCollection<BackUpControlViewModel>), typeof(MainWindowViewModel));
public ObservableCollection<BackUpControlViewModel> FtpControlList
{
get { return (ObservableCollection<BackUpControlViewModel>)GetValue(FtpControlListProperty); }
set { SetValue(FtpControlListProperty, value); }
}
Run Code Online (Sandbox Code Playgroud)
现在由于某种原因,它加载了3次空的usercontrol而不是FtpControlList属性中的属性设置为"View 1,View 2和View 3".如何确保列表中的UserControls已加载而非空载?
UserControlViewModel的一部分:
// part of the UserControl …Run Code Online (Sandbox Code Playgroud) 当我尝试向集合(或任何更改集合的操作)添加/插入/删除时,我得到以下异常.初始化集合,插入的项目不为null,与集合T的类型相同.
谁能给我一个线索,说明为什么会这样?
运行时遇到了致命错误.错误的地址是在0x60f41744线程上0x231c.错误代码是0x80131623.
此错误可能是CLR中的错误,也可能是用户代码的不安全或不可验证部分中的错误.此错误的常见来源包括COM-interop或PInvoke的用户编组错误,这可能会破坏堆栈.
更新:该集合是一个ObservableCollection,我设法得知它发生在集合的通知部分发生了变化.
这发生在具有该TaskScheduler.FromCurrentSynchronizationContext()选项的任务内的UI线程上.
奇怪的是如果我删除this(TaskScheduler.FromCurrentSynchronizationContext())选项add/insert/remove动作,一切似乎都很好.

对不起,如果标题含糊不清,但这正是我想要实现的.
这是Battery Care软件图标化/最小化.将鼠标悬停在图标上时,您将看到图片中显示的窗口.
如何在Java中实现?
我有一个WPF应用程序,我可以最小化托盘.当我正常点击它时,窗口再次显示.
现在我想知道如何创建一个简单的ContextMenu?
将ContextMenu要得到充满了的onclick将运行一个函数x选项.现在我只需要一个'Exit'项链接到'Exit_Click'方法.
我尝试过的是:
ContextMenu menu = (ContextMenu)this.FindResource("NotifierContextMenu");
menu.IsOpen = true;
Run Code Online (Sandbox Code Playgroud)
menu不知道任何IsOpen价值.
其他例子喜欢使用很多不同的东西.其中一个要求我出于某种原因创建HostManager.
我只需要一个简单的ContextMenu.我怎样才能做到这一点?
我试图获得一些简单的功能,从文件中获取图像,将其添加到Canvas,然后允许用户左键单击(并保持)图像,然后将其拖动到Canvas(即更新图像的地点)
这是我到目前为止,我应该添加什么?
private void btnAddImage_Click(object sender, RoutedEventArgs e) {
try {
System.Windows.Forms.OpenFileDialog open = new System.Windows.Forms.OpenFileDialog();
open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
if (open.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
PictureBox PictureBox1 = new PictureBox();
PictureBox1.Image = new Bitmap(open.FileName);
myCanvas.children.add(PictureBox1);
}
}
catch (Exception) { throw new ApplicationException("Failed loading image"); }
}
Run Code Online (Sandbox Code Playgroud) 有人可以告诉我如何从当前位置为窗口设置动画。我正在寻找一种摇动效果,它可以简单地左右摇晃窗户 5 到 6 次。
我知道我需要使用 Animation.By。这是我已经开始但还没有走远的事情。
但是这不起作用。
<Storyboard x:Key="sbShake1" FillBehavior="Stop">
<DoubleAnimation Storyboard.TargetName="W1" Storyboard.TargetProperty ="(Window.Left)"
By="10" Duration="0:0:1">
</DoubleAnimation >
</Storyboard >
Run Code Online (Sandbox Code Playgroud)
我设法获得了正确的抖动效果,但我无法从 Windows 当前位置做到这一点。
<Storyboard x:Key="sbShake" RepeatBehavior ="00:00:01" SpeedRatio ="25" >
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty ="Left">
<SplineDoubleKeyFrame KeyTime ="00:00:00.1000000" Value ="-10"/>
<SplineDoubleKeyFrame KeyTime ="00:00:00.3000000" Value ="0"/>
<SplineDoubleKeyFrame KeyTime ="00:00:00.5000000" Value ="10"/>
<SplineDoubleKeyFrame KeyTime ="00:00:00.7000000" Value ="0"/>
</DoubleAnimationUsingKeyFrames >
</Storyboard >
Run Code Online (Sandbox Code Playgroud)
所有帮助将不胜感激。
我有一个TreeView项目,我想要ContextMenu只弹出第二层项目.我该怎么做呢?
wpf ×8
c# ×7
contextmenu ×2
wpf-controls ×2
xaml ×2
actualheight ×1
actualwidth ×1
animation ×1
datagrid ×1
exception ×1
image ×1
itemscontrol ×1
java ×1
mouseevent ×1
mouseover ×1
mvvm ×1
popup ×1
swing ×1
system-tray ×1
tray ×1
treeview ×1
visual-tree ×1