小编Bre*_*ent的帖子

在样式中定义InputBindings

我正在使用MVVM设计模式创建一个WPF应用程序,我正在尝试扩展TabItem控件,以便在用户单击鼠标中键时关闭选项卡.我正在尝试使用InputBindings实现这一点,并且在我尝试在样式中定义它之前它非常有效.我已经了解到,除非使用DependencyProperty附加,否则无法将InputBindings添加到样式中.所以我在这里跟着这个类似的帖子......它几乎可以工作.我可以使用鼠标中键关闭一个选项卡,但它不能在任何其他选项卡上运行(所有选项卡都在运行时添加并继承相同的样式).

所以我需要一些帮助.为什么这只会在第一次工作,而不是之后?显然,我可以创建一个继承自TabItem的自定义控件并使其工作,但我想弄清楚这一点,因为我可以看到它在我的项目中被扩展.我不是DependencyProperties的专家,所以请帮帮我.谢谢!

样式:

<Style TargetType="{x:Type TabItem}">
    <Setter Property="w:Attach.InputBindings">
        <Setter.Value>
            <InputBindingCollection>
                <MouseBinding MouseAction="MiddleClick" 
                              Command="{Binding CloseCommand}"/>
            </InputBindingCollection>
        </Setter.Value>
    </Setter>
    ...
</Style>
Run Code Online (Sandbox Code Playgroud)

public class Attach
{
    public static readonly DependencyProperty InputBindingsProperty =
        DependencyProperty.RegisterAttached("InputBindings", typeof(InputBindingCollection), typeof(Attach),
        new FrameworkPropertyMetadata(new InputBindingCollection(),
        (sender, e) =>
        {
            var element = sender as UIElement;
            if (element == null) return;
            element.InputBindings.Clear();
            element.InputBindings.AddRange((InputBindingCollection)e.NewValue);
        }));

    public static InputBindingCollection GetInputBindings(UIElement element)
    {
        return (InputBindingCollection)element.GetValue(InputBindingsProperty);
    }

    public static void SetInputBindings(UIElement element, InputBindingCollection inputBindings)
    {
        element.SetValue(InputBindingsProperty, inputBindings);
    }
}
Run Code Online (Sandbox Code Playgroud)

wpf dependency-properties mvvm inputbinding

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

在ListView中显示分组项目的总和

我正在使用MVVM设计模式创建一个WPF TimeCard应用程序,我正在尝试显示用户按天数分组的总和(总)小时数.我有一个ListView,使用以下XAML将所有TimeCard数据分组:

<ListView.GroupStyle>
    <GroupStyle ContainerStyle="{StaticResource GroupItemStyle}">
        <GroupStyle.HeaderTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Path=Name, StringFormat=\{0:D\}}" FontWeight="Bold"/>
                    <TextBlock Text="  (" FontWeight="Bold"/>
                    <!-- This needs to display the sum of the hours -->
                    <TextBlock Text="{Binding ???}" FontWeight="Bold"/>
                    <TextBlock Text=" hours)" FontWeight="Bold"/>
                </StackPanel>
            </DataTemplate>
        </GroupStyle.HeaderTemplate>
    </GroupStyle>
</ListView.GroupStyle>
Run Code Online (Sandbox Code Playgroud)

这甚至可能吗?起初我以为我会创建一个CollectionViewGroup的部分类并添加我自己的属性.但我不确定这是否会奏效.也许有更好的解决方案......任何建议?

wpf grouping listview mvvm listcollectionview

9
推荐指数
1
解决办法
9202
查看次数

阻止WPF帧在堆栈中存储历史记录

这似乎是一个简单的解决方案,但我浪费了太多时间试图解决这个问题.也许我正在设计我的应用程序(可能是这种情况),所以如果你有更好的解决方案,请帮助我.

我正在设计一个企业级WPF应用程序,它看起来很像Outlook Ribbon而不是工具栏.当用户点击a时,我有很多不同的模块被加载到一个框架中RibbonButton.请记住,他的功能区在所有模块中共享.

所以我有一个带有色带和框架的外壳.当用户单击功能区按钮时,它会将正确的模块(usercontrol)加载到框架中.一切都很好.但是,如果我导航到另一个模块(通过单击另一个RibbonButton),然后单击原始模块RibbonButton,我现在有两个相同模块的实例打开...但框架中只显示一个...另一个模块在框架的堆栈中.

所以我的问题是,当我导航到另一个模块时,如何告诉框架关闭用户控件?我试过设置JournalEntry.KeepAlive="False",但仍然无法正常工作.有什么想法吗?实际上没有太多的代码可以发布,但如果能帮到你,我可以这么做.

wpf mvvm composite-application

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