Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 静态绑定可以编译,但不能在设计器下工作_Wpf_Xaml_Data Binding_Binding - Fatal编程技术网

Wpf 静态绑定可以编译,但不能在设计器下工作

Wpf 静态绑定可以编译,但不能在设计器下工作,wpf,xaml,data-binding,binding,Wpf,Xaml,Data Binding,Binding,下面的代码在编译时可以正常工作,但我无法让文本={x:Static local:SomeClass+Limits.Name}在设计器中工作。在此方面的任何帮助都将不胜感激!谢谢 namespace StaticTest { public class SomeClass { public static class Limits { public const string Name = "It Works!";

下面的代码在编译时可以正常工作,但我无法让文本={x:Static local:SomeClass+Limits.Name}在设计器中工作。在此方面的任何帮助都将不胜感激!谢谢

namespace StaticTest
{
    public class SomeClass 
    {
        public static class Limits
        {
            public const string Name = "It Works!";
        }
    }
}

您需要一个绑定,如@H.B.所示:

<TextBlock Text="{Binding Source={x:Static local:SomeClass+Limits.Name}}"/>

VS2010设计器无法处理此问题,但它在VS11测试版中运行良好。

我知道了。事实证明,您需要执行以下操作:

using System;

namespace StaticTest
{
    public abstract class AbstractViewModel<VM, LC>
        where VM : new()
        where LC : new()
    {
        public AbstractViewModel()
        {
            Limits = new LC();
        }

        static AbstractViewModel()
        {
            Instance = new VM();
        }

        public LC Limits { get; private set; }

        public static VM Instance { get; private set; }
    }

    public class MainWindowViewModel :
        AbstractViewModel<MainWindowViewModel, MainWindowViewModel.LimitsClass>
    {
        public class LimitsClass
        {
            public LimitsClass()
            {
                Lots = new MinMax<int>(1, 10);
            }

            public MinMax<int> Lots { get; private set; }
        }        
    }
}


<Window
    x:Class="StaticTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:StaticTest"
    Title="StaticTest"
    Height="146"
    Width="296"
    WindowStartupLocation="CenterScreen">
    <Grid>
        <TextBlock
            Text="{Binding Source={x:Static local:MainWindowViewModel.Instance}, Path=Limits.Lots.MaxValue}" />
    </Grid>
</Window>

静态不是绑定。谢谢你的建议!不幸的是,它没有在设计器中编译,但如果我按F5,它会运行,并说找不到类型“local:SomeClass+Limits”。@lsb,它对我有效,但可能是因为我使用的是VS1l beta。明天我将在VS2010中试用。
using System;

namespace StaticTest
{
    public abstract class AbstractViewModel<VM, LC>
        where VM : new()
        where LC : new()
    {
        public AbstractViewModel()
        {
            Limits = new LC();
        }

        static AbstractViewModel()
        {
            Instance = new VM();
        }

        public LC Limits { get; private set; }

        public static VM Instance { get; private set; }
    }

    public class MainWindowViewModel :
        AbstractViewModel<MainWindowViewModel, MainWindowViewModel.LimitsClass>
    {
        public class LimitsClass
        {
            public LimitsClass()
            {
                Lots = new MinMax<int>(1, 10);
            }

            public MinMax<int> Lots { get; private set; }
        }        
    }
}


<Window
    x:Class="StaticTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:StaticTest"
    Title="StaticTest"
    Height="146"
    Width="296"
    WindowStartupLocation="CenterScreen">
    <Grid>
        <TextBlock
            Text="{Binding Source={x:Static local:MainWindowViewModel.Instance}, Path=Limits.Lots.MaxValue}" />
    </Grid>
</Window>