小编JCH*_*H2k的帖子

如何为样式指定设计器datacontext,以便Resharper找到我的属性?

我经常将TreeViewItem的IsExpanded和IsSelected属性绑定到我的viewmodel.例如,这使得可以在加载树时使项目预扩展,或者在选择树时扩展项目.

XAML看起来像这样:

<Window x:Class="StyleSetterDatatypeTest.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
            xmlns:test="clr-namespace:StyleSetterDatatypeTest"
            Title="MainWindow" Height="350" Width="525"
            mc:Ignorable="d"
            d:DataContext="{d:DesignInstance test:TestViewModel, IsDesignTimeCreatable=True}">

    <TreeView ItemsSource="{Binding Items}">
        <TreeView.Resources>
            <Style TargetType="TreeViewItem">
                <Setter Property="IsExpanded" Value="{Binding ItemExpanded}"/>
                <Setter Property="IsSelected" Value="{Binding ItemSelected}"/>
            </Style>

            <HierarchicalDataTemplate DataType="{x:Type test:TestItemViewModel}" ItemsSource="{Binding Children}">
                <TextBlock Text="{Binding Name}"/>
            </HierarchicalDataTemplate>
        </TreeView.Resources>
    </TreeView>
</Window>
Run Code Online (Sandbox Code Playgroud)

我的viewmodel看起来像这样:

public class TestItemViewModel
{
    public bool ItemExpanded { get; set; }

    public bool ItemSelected { get; set; }

    public string Name { get; set; }

    public string[] Children
    {
        get { return new [] {"Child 1", …
Run Code Online (Sandbox Code Playgroud)

c# wpf resharper xaml mvvm

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

ToggleButton 动画的启动状态

我想为 ToggleButton 创建一个动画“切换”样式(就像在智能手机上一样)。

动画本身不是问题,当我点击它时,开关看起来不错并且动画。但它也会在加载并打开时进行动画处理。

当我加载我的视图时,它应该显示一些开关打开和一些关闭。但它只显示“关闭”并自动启动“打开”动画。

下面的简单代码显示了我的问题:在选中按钮时,我将 Button 设置为 150px 宽,而不是开关。

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Class="AnimationTest2.MainWindow"
        Title="MainWindow" Height="350" Width="525">

    <Window.Resources>
        <Style TargetType="{x:Type ToggleButton}" BasedOn="{StaticResource {x:Type ToggleButton}}" x:Key="SwitchStyle">
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsChecked, RelativeSource={RelativeSource Self}}" Value="true">
                    <DataTrigger.EnterActions>
                        <BeginStoryboard>
                            <Storyboard Storyboard.TargetProperty="Width">
                                <DoubleAnimation To="150"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </DataTrigger.EnterActions>
                    <DataTrigger.ExitActions>
                        <BeginStoryboard>
                            <Storyboard Storyboard.TargetProperty="Width">
                                <DoubleAnimation To="50"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </DataTrigger.ExitActions>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>

    <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
        <ToggleButton Width="50" Content="Button" Style="{StaticResource ResourceKey=SwitchStyle}"/>
        <ToggleButton Width="50" Content="Button" Style="{StaticResource ResourceKey=SwitchStyle}" IsChecked="True"/>
    </StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)

第一个 ToggleButton 运行良好。单击时未选中并设置为 150px 的动画。默认情况下选中第二个 ToggleButton 并且应该立即为 150px,但是当我启动我的应用程序时它开始增长!

不知何故,我需要根据 IsChecked …

wpf

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

MVVM 绑定在设计时不起作用

我试图将几个按钮的可见性绑定到视图模型中的一些布尔值,并使其在设计时工作。我做了几次,从来没有遇到过问题,但现在它不起作用,我不知道为什么。请注意,当我运行应用程序时,一切正常。

我将精华提取到一个单独的应用程序中,它仍然不起作用!

我的 XAML:

<Window x:Class="BindingTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:bindingTest="clr-namespace:BindingTest"
        Title="MainWindow" Height="350" Width="525"
        mc:Ignorable="d"
        d:DataContext="{d:DesignInstance bindingTest:TestViewModel, d:IsDesignTimeCreatable=true}">
    <Grid>
        <Grid.Resources>
            <BooleanToVisibilityConverter x:Key="BoolToVis"/>
        </Grid.Resources>
        <StackPanel>
            <Button Visibility="{Binding IsButton1Visible, Converter={StaticResource BoolToVis}}">Hallo 1</Button>
            <Button Visibility="{Binding Button2Visibility}">Hallo 2</Button>
            <Button>Hallo 3</Button>
        </StackPanel>
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

我的视图模型:

public class TestViewModel
{
    public bool IsButton1Visible
    {
        get { return true; }
    }

    public Visibility Button2Visibility
    {
        get { return Visibility.Hidden; }
    }
}
Run Code Online (Sandbox Code Playgroud)

为了让它在真正的应用程序中工作,我添加到 XAML 的构造函数中:

DataContext = new TestViewModel();
Run Code Online (Sandbox Code Playgroud)

这一切都很简单,但为什么在设计器中不起作用??过去我总是让这个工作,现在我尝试了几个小时......这不是唯一不工作的绑定,但为什么连这个都不起作用?

预期结果:

  • Button1 可见
  • Button2 隐藏
  • Button3 可见 …

c# wpf xaml mvvm

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

ViewModel中计时器的更好解决方案?

我在ViewModel的图形模型中有一个DispatcherTimer,用于定期更新(滚动)它。

最近,我发现这是一个巨大的资源泄漏,因为每次我导航到图形视图时都会重新创建ViewModel,并且DispatcherTimer阻止了GC破坏我的ViewModel,因为Tick-Event拥有强大的引用。

我与包装解决了这个围绕它使用的DispatcherTimer FastSmartWeakEvent从CodeProject /丹尼尔·格伦沃尔德以避免强烈的参考VM和破坏自己一旦有没有更多的听众:

public class WeakDispatcherTimer
{
    /// <summary>
    /// the actual timer
    /// </summary>
    private DispatcherTimer _timer;



    public WeakDispatcherTimer(TimeSpan interval, DispatcherPriority priority, EventHandler callback, Dispatcher dispatcher)
    {
        Tick += callback;

        _timer = new DispatcherTimer(interval, priority, Timer_Elapsed, dispatcher);
    }


    public void Start()
    {
        _timer.Start();
    }


    private void Timer_Elapsed(object sender, EventArgs e)
    {
        _tickEvent.Raise(sender, e);

        if (_tickEvent.EventListenerCount == 0) // all listeners have been garbage collected
        {
            // kill the timer once the last listener is gone
            _timer.Stop(); …
Run Code Online (Sandbox Code Playgroud)

c# wpf mvvm scichart

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

标签 统计

wpf ×4

c# ×3

mvvm ×3

xaml ×2

resharper ×1

scichart ×1