【问题标题】:Static Binding works compiled but not under designer静态绑定作品已编译但不在设计者之下
【发布时间】:2012-04-20 22:45:40
【问题描述】:

以下代码在编译时工作正常,但我无法让 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>

【问题讨论】:

  • x:Static 不是绑定。

标签: wpf xaml data-binding binding


【解决方案1】:

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

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>

【讨论】:

    【解决方案2】:

    您需要绑定为@H.B.说:

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

    VS2010 设计器无法处理此问题,但在 VS11 beta 中可以正常工作。

    【讨论】:

    • 感谢您的建议!不幸的是,它没有在设计器中编译(尽管如果我按 F5 会运行),说“找不到类型'local:SomeClass+Limits'。”
    • @lsb,它对我有用,但可能是因为我使用的是 VS1l beta。我明天在VS2010上试试。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-10
    • 2012-04-19
    • 1970-01-01
    • 2020-01-04
    • 1970-01-01
    • 1970-01-01
    • 2014-03-30
    相关资源
    最近更新 更多