小编Met*_*tro的帖子

如何将WPF ScrollViewer的内容滚动到特定位置

我正在编写自定义WPF ItemsControl以显示项目列表.这些项目显示在ScrollViewer中:

<Style TargetType="MyCustomItemsControl">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="MyCustomItemsControl">
                    <ScrollViewer x:Name="PART_MyScrollViewer" >
                           <ItemsPresenter/>
                    </ScrollViewer>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)

我想确保当我将鼠标移动到控件中时,特定项目(标记为已选中)将滚动到鼠标位置.在我的OnMouseEnter方法中,我能够找到该项目,但我不知道下一步该做什么.有谁有想法吗?

protected override void OnMouseEnter(MouseEventArgs e)
{
    for (int i = 0; i < Items.Count; i++)
    {
        ContentPresenter uiElement = (ContentPresenter)ItemContainerGenerator.ContainerFromIndex(i);
        var item = uiElement.Content as MyCustomObject;
        if (item.IsSelected)
        {
            // How to scroll the uiElement to the mouse position?
            break;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

wpf mouse position scrollviewer

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

在WPF中创建条纹画笔

如何为条纹创建画笔(DrawingBrush),如博客中所示:

http://blog.pixelingene.com/2008/09/quick-tip-to-get-a-striped-background/

我无法使用它,因为它使用了缩放变换,这意味着如果UI元素很小,条纹几乎不可见或太靠近.

我不能使用图像画笔,因为我需要绑定颜色.

wpf brush

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

绑定显示double为int

我想在我的控制中以分钟显示持续时间,但我不希望它显示为十进制数字(例如,65而不是65.94503).

<TextBlock Text="{Binding Duration.TotalMinutes, StringFormat=\{0\} minutes}" />
Run Code Online (Sandbox Code Playgroud)

我该怎么做?

wpf binding

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

无法在运行时找到资源字典文件

我正在开发一个用户控件并在ElementHost中使用它.我定义资源字典如下:

<UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/Themes/Classic.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary> 
    </UserControl.Resources>
Run Code Online (Sandbox Code Playgroud)

在我的VS资源管理器中,我有这个

Project (the user control library)
  |_ Themes
       |__ Generic.xaml  (Build Action = Page)
       |__ Classic.xaml  (Build Action = Page)
Run Code Online (Sandbox Code Playgroud)

没有编译错误,VS设计师似乎拿起了Classic.xaml中定义的资源

但是,它在运行时崩溃,但有以下异常:

System.Reflection.TargetInvocationException:调用目标抛出了异常.---> System.Reflection.TargetInvocationException:调用目标抛出了异常.---> System.Reflection.TargetInvocationException:调用目标抛出了异常.---> System.Windows.Markup.XamlParseException:'Set property'System.Windows.ResourceDictionary.Source'抛出异常.' 行号"16"和行位置"18".---> System.IO.IOException:找不到资源'themes/classic.xaml'.

这是怎么回事?

wpf themes

4
推荐指数
1
解决办法
6211
查看次数

自定义扩展器以扩展鼠标输入

我在WPF中使用Expander来显示我的数据.Expander控件的默认样式包含一个切换按钮,当我单击它时显示/隐藏我的内容.

当我将鼠标悬停在标题上并在我离开时崩溃时,如何修改样式以使其扩展?

wpf mouse enter expander

3
推荐指数
1
解决办法
3132
查看次数

标签 统计

wpf ×5

mouse ×2

binding ×1

brush ×1

enter ×1

expander ×1

position ×1

scrollviewer ×1

themes ×1