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>
4

2 回答 2

0

I got it. As it turns out you need to do something like the following:

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>
于 2012-04-21T19:54:27.153 回答
0

正如@HB 所说,您需要一个绑定:

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

VS2010 设计器无法处理这个问题,但它在 VS11 beta 中运行良好。

于 2012-04-21T15:27:08.467 回答