小编cpl*_*tts的帖子

如何使WPF滑块仅捕捉到离散的整数位置?

我经常需要一个WPF滑块,其行为类似于旧的System.Windows.Forms.TrackBar.也就是说,我想要一个从X到Y的滑块,但只允许用户在离散的整数位置移动它.

如何在WPF中执行此操作,因为Slider上的Value属性是双倍的?

wpf controls slider

111
推荐指数
4
解决办法
8万
查看次数

清除ObservableCollection时,e.OldItems中没有项目

我这里有些东西让我措手不及.

我有一个带有项目的Ob的ObservableCollection.我还有一个附加到CollectionChanged事件的事件处理程序.

当您清除集合,将导致以e.Action的CollectionChanged事件设置为NotifyCollectionChangedAction.Reset.好的,这很正常.但奇怪的是,e.OldItems或e.NewItems都没有任何内容.我希望e.OldItems可以填充从集合中删除的所有项目.

有没有人见过这个?如果是这样,他们是如何绕过它的?

一些背景:我使用CollectionChanged事件来附加和分离另一个事件,因此如果我没有在e.OldItems中获取任何项目......我将无法从该事件中分离出来.


澄清: 我知道文档并没有完全声明它必须以这种方式行事.但对于其他所有行动,它都会告诉我它做了什么.所以,我的假设是它会告诉我......在清除/重置的情况下.


如果您希望自己重现,下面是示例代码.首先关闭xaml:

<Window
    x:Class="ObservableCollection.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1"
    Height="300"
    Width="300"
>
    <StackPanel>
        <Button x:Name="addButton" Content="Add" Width="100" Height="25" Margin="10" Click="addButton_Click"/>
        <Button x:Name="moveButton" Content="Move" Width="100" Height="25" Margin="10" Click="moveButton_Click"/>
        <Button x:Name="removeButton" Content="Remove" Width="100" Height="25" Margin="10" Click="removeButton_Click"/>
        <Button x:Name="replaceButton" Content="Replace" Width="100" Height="25" Margin="10" Click="replaceButton_Click"/>
        <Button x:Name="resetButton" Content="Reset" Width="100" Height="25" Margin="10" Click="resetButton_Click"/>
    </StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)

接下来,代码背后:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media; …
Run Code Online (Sandbox Code Playgroud)

wpf observablecollection

89
推荐指数
6
解决办法
4万
查看次数

您可以在Style或ControlTemplate中包含的Storyboard中使用DynamicResource吗?

我试图在ControlTemplate中包含的Storyboard中使用DynamicResource.

但是,当我尝试这样做时,我得到一个'无法冻结此Storyboard时间轴树以供跨线程使用'错误.

这里发生了什么?

wpf animation

13
推荐指数
1
解决办法
6131
查看次数

如何以延迟打开WPF弹出窗口?

我只是想延迟打开WPF Popup,有点像ToolTip.

我怎样才能做到这一点?

顺便说一下,Popup.PopupAnimation = PopupAnimation.Fade ......过快地消失了.我想在那里至少半秒钟.

wpf popup delay wpf-controls

13
推荐指数
3
解决办法
2万
查看次数

为什么WPF中的TextBox.Text不可动画?

好吧,我刚刚遇到了让我措手不及的事情.

我正在帮助一位开发人员提出几个不相关的问题,在他的项目中,他正在将文本设置为一些TextBlock(s).所以,我回到我的办公桌并重新创建项目(为了回答他的问题),但我不小心使用了TextBox而不是TextBlock.我的文字根本没有动画!(很多帮助,我是!)

最后,我发现他的xaml正在使用TextBlock而我的正在使用TextBox.有趣的是,当我使用TextBox时,Blend没有创建关键帧.因此,我使用TextBlock在Blend中工作,然后手动修改xaml,将TextBlock转换为TextBox(es).当我运行该项目时,我收到以下错误:

InvalidOperationException: '(0)' Storyboard.TargetProperty path contains nonanimatable property 'Text'.

好吧,似乎Blend足够聪明,知道......并且没有在动画中生成关键帧(它只是直接修改TextBox上的值).混合+1.

所以,问题变成了:为什么TextBox.Text不是可动画的?通常的答案是您动画的特定属性不是DependencyProperty.但是,情况并非如此,TextBox.Text DependencyProperty.

所以,现在我很困惑!你为什么不能动画TextBox.Text?


让我包括一些xaml来说明问题.以下xaml工作...但使用TextBlock.

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="TextBoxTextQuestion.MainWindow"
    x:Name="Window"
    Title="MainWindow"
    Width="640"
    Height="480"
>
    <Window.Resources>
        <Storyboard x:Key="animateTextStoryboard">
            <StringAnimationUsingKeyFrames Storyboard.TargetProperty="(TextBlock.Text)" Storyboard.TargetName="textControl">
                <DiscreteStringKeyFrame KeyTime="0:0:1" Value="Goodbye"/>
            </StringAnimationUsingKeyFrames>
        </Storyboard>
    </Window.Resources>
    <Window.Triggers>
        <EventTrigger RoutedEvent="FrameworkElement.Loaded">
            <BeginStoryboard Storyboard="{StaticResource animateTextStoryboard}"/>
        </EventTrigger>
    </Window.Triggers>
    <Grid x:Name="LayoutRoot">
        <StackPanel Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center">
            <TextBlock x:Name="textControl" Text="Hello" FontFamily="Calibri" FontSize="32"/>
            <TextBlock Text="World!" Margin="0,25,0,0" FontFamily="Calibri" FontSize="32"/>
        </StackPanel>
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

以下xaml 不起作用并使用TextBox.Text:

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="TextBoxTextQuestion.MainWindow"
    x:Name="Window"
    Title="MainWindow"
    Width="640" …
Run Code Online (Sandbox Code Playgroud)

wpf animation text textbox textblock

10
推荐指数
1
解决办法
4944
查看次数

使用.NET Reflector Pro的经验

有没有人有.NET Reflector Pro(不是免费版)的经验?

我已经搜索了一些在外部环境中轻松调试应用程序的方法(例如,不在办公室的生产服务器),我找到了.NET Reflector Pro及其功能"步入反编译程序集并使用所有调试技术会使用你自己的代码".

它看起来非常有用,但我想听听有关该工具的个人经验......特别是那些"每天"使用它的人.

谢谢!

编辑: 一些额外的信息:我想使用此功能来模拟模块化应用程序的旧版本(多个版本中的100多个模块),以便难以重现未在测试环境中显示的错误(例如组合数据库中的数据,服务器/客户端的当前配置等.

有一种方法可以重建指定的程序集(有时需要几十个程序集)...添加一些特定于问题的日志记录信息.但是,它需要很长时间,而不是轻松调试和查看局部变量,字段,线程等内容.

.net debugging reflector

8
推荐指数
2
解决办法
4062
查看次数

为什么我的Popup在某些机器上显示与Placement属性相反?

我有一个简单的WPF Popup,当用户点击Button时我会显示它.

<Button
    x:Name="aButton"
    Content="Up/Down"
    Width="75"
    Height="30"
    Click="aButton_Click"
/>
<Popup
    PlacementTarget="{Binding ElementName=aButton}"
    Placement="Right"
    VerticalOffset="-31"
    StaysOpen="False"
    AllowsTransparency="True"
>
    <StackPanel>
        <Button Width="45" Height="45" Margin="2,0,2,2" Content="+"/>
        <Button Width="45" Height="45" Margin="2,0,2,0" Content="-"/>
    </StackPanel>
</Popup>
Run Code Online (Sandbox Code Playgroud)

非常奇怪的是......这段代码的运行方式不同,具体取决于它运行的机器.

我在我的主桌面上运行此代码,一切正常......并且应该如此.我在我的PDC09上网本上运行它......弹出窗口显示相反(左侧而不是右侧,因为我告诉它使用Placement属性).

为什么是这样?我能做些什么呢?

wpf popup

8
推荐指数
1
解决办法
1512
查看次数

你在WPF工作中使用了哪些设计模式,你喜欢哪种?

我一直在寻找那里有几个人建议的Model-View-ViewModel模式(特别是John Gossman,看一下这篇文章和这个播客),但是其他模式(如果有的话)让人们使用和喜欢. ..他们在哪里增加价值?

我也偶然发现:

我希望能够积极讨论上述这些以及我可能尚未发现的内容.

wpf tdd mvp design-patterns

5
推荐指数
1
解决办法
3003
查看次数

将DynamicResource用于应用程序级资源可能会导致泄漏

似乎使用DynamicResource来引用应用程序级资源可能会导致内存泄漏.

有关更多信息,如何重现它以及一些解决方法,请参阅此WPF论坛帖子.

我的问题是:还有其他人遇到过吗?如果是这样,你是如何解决它的?

顺便说一句,似乎有很多情况下没有发生这种泄漏,也许最好的问题是:这种泄漏发生和不发生的情况究竟是什么?

为方便起见,这里是重现它的代码:

App.xaml中

<Application
    x:Class="WeakReferences.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="Window1.xaml"
>
    <Application.Resources>
        <SolidColorBrush x:Key="MyBrush" Color="SkyBlue"/>
    </Application.Resources>
</Application>
Run Code Online (Sandbox Code Playgroud)

Window1.xaml

<Window
    x:Class="WeakReferences.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1"
    Height="300"
    Width="300"
>
    <Grid>
        <Button
            Name="ReleaseButton"
            Content="Release Reference"
            Click="Button_Click"
        />
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

Window1.xaml.cs

public partial class Window1 : Window
{
    object p;

    public Window1()
    {
        InitializeComponent();

        p = new Page1();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {            
        p = null;

        GC.Collect();
    }
}
Run Code Online (Sandbox Code Playgroud)

的Page1.xaml

<Page
    x:Class="WeakReferences.Page1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Page1"
    Background="{DynamicResource MyBrush}"
>
    <Grid> …
Run Code Online (Sandbox Code Playgroud)

wpf resources memory-leaks

5
推荐指数
1
解决办法
1005
查看次数

什么时候应该在WPF中提取UserControl

在我们的工作中,我们有一个争议正在进行,有些人想要创建用户控件,结合常用的控件,如标签和文本框,或标签和图像控件.就是这样的:

<StackPanel Orientation="Horizontal">
    <Image Source="/Someimage/Somewhere.gif"/>
    <Label>Some text, hyperlink, or other content</Label>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)

并像这样使用它

<ImageLabel
    HeaderImageSource="/Someimage/Somewhere.gif"
>
    Some text, hyperlink, or other content
</ImageLabel>
Run Code Online (Sandbox Code Playgroud)

问题是,他们是否提供足够的封装和抽象来值得单独的用户控制?

wpf user-controls

5
推荐指数
1
解决办法
295
查看次数

如何使用Chrome和点击断点调试Silverlight应用程序?

我正在使用Visual Studio 2010来创建Silverlight 4应用程序.

我在代码隐藏中设置了一个断点,从Visual Studio启动调试会话,不幸的是,我的断点永远不会被击中.

所以,我最终尝试将我的默认浏览器设置为Internet Explorer ...并且看到......我的断点突然被击中.

Chrome是否是支持调试Silverlight应用程序的浏览器?如果是这样,为了让这个工作起来,我错过了什么?

或者,Internet Explorer是调试时唯一受支持的浏览器吗?

browser debugging silverlight google-chrome breakpoints

5
推荐指数
1
解决办法
4347
查看次数

WPF Snoop无法在Windows XP上运行?

有没有人注意到WPF开发者最好的朋友Snoop没有在XP上工作?我也试过Snooping Expression Blend(一个WPF应用程序)和其他WPF应用程序.Snoop找到它们但是在按下双筒望远镜按钮时没有启动窗口.

编辑:我显然运行XP SP2(支持WPF).

-pom-

wpf utilities

2
推荐指数
1
解决办法
1899
查看次数