2012-04-20 25 views
0

下面的代码在编译时工作正常,但我无法获得Text =“{x:Static local:SomeClass + Limits.Name}”在设计器中工作。任何在这方面的帮助将不胜感激!谢谢....静态绑定作品编译但不在设计器下

namespace StaticTest 
{ 
    public class SomeClass 
    { 
     public static class Limits 
     { 
      public const string Name = "It Works!"; 
     } 
    } 
} 
<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"> 
    <TextBlock 
     Grid.Column="1" 
     Grid.Row="1" 
     Text="{x:Static local:SomeClass+Limits.Name}" /> 
</Window> 
+3

'x:Static'是*不是*绑定。 – 2012-04-21 01:14:56

回答

0

你需要一个绑定,@ H.B。说:

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

VS2010的设计者不能处理这个,但它在VS11测试版工作正常。

+0

感谢您的建议!不幸的是,它没有在设计器中编译(虽然它按照F5运行),并说“没有找到Type'local:SomeClass + Limits'”。 – 2012-04-21 19:02:48

+0

@lsb,它适用于我,但可能是因为我正在使用VS1l测试版。我会在明天VS2010中尝试它。 – Phil 2012-04-21 20:08:13

+0

是的,它在VS11 beta中找到,但在VS2010中不起作用。 – Phil 2012-04-22 19:41:47

0

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

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>