【问题标题】:Why does this binding doesn't work through XAML but does by code?为什么此绑定不能通过 XAML 工作,而是通过代码工作?
【发布时间】:2011-03-01 10:27:56
【问题描述】:

我正在尝试绑定到静态类的静态属性, 此属性包含从文件反序列化的设置。

它永远不会与以下 XAML 一起使用:

    <Window.Resources>
    <ObjectDataProvider x:Key="wrapper" ObjectType="{x:Type Application:Wrapper}"/>
</Window.Resources>

<ScrollViewer x:Name="scrollViewer" ScrollViewer.VerticalScrollBarVisibility="Auto"DataContext="{Binding Source={StaticResource wrapper}, UpdateSourceTrigger=PropertyChanged}">

   <ComboBox x:Name="comboboxThemes"
                  SelectedIndex="0"
                  SelectionChanged="ComboBoxThemesSelectionChanged"
                  Grid.Column="1"
                  Grid.Row="8"
                  Margin="4,3" ItemsSource="{Binding Settings.Themes, Mode=OneWay}" SelectedValue="{Binding Settings.LastTheme, Mode=TwoWay}"   />

它确实可以通过代码工作:

comboboxThemes.ItemsSource = Settings.Themes;

有什么想法吗?

谢谢你:-)

【问题讨论】:

    标签: c# wpf binding static properties


    【解决方案1】:

    您的代码隐藏不执行绑定,它直接将源分配给ComboBox...

    如果你想在 XAML 中做同样的事情,你根本不需要绑定,你只需要 StaticExtension 标记扩展:

    ItemsSource="{x:Static local:Settings.Themes}"
    

    (其中local 是包含Settings 类的命名空间的xmlns 映射)

    【讨论】:

      【解决方案2】:

      XAML:

      <Window x:Class="StaticTest.Window1"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:StaticTest="clr-namespace:StaticTest"
          Height="300" Width="300">
          <StackPanel>
              <TextBlock Text="{x:Static StaticTest:MyStaticStuff.MyProp}" />
          </StackPanel>
      </Window>
      

      后面的代码:

      namespace StaticTest
      {
          public static class MyStaticStuff
          {
              public static string MyProp { get { return "From static"; } }
          }
      }
      

      【讨论】:

        【解决方案3】:

        我已经找到答案了!

        它确实默默地抛出了一个异常已被调用的目标抛出我不知道更多......

        我正在初始化一个写入文件的日志;设计者终于显示了异常的详细信息,它正在寻找在Program Files中的Visual Studio目录中创建文件,因此引发了安全异常。

        显然 VS 将文件复制到其文件夹中,以供其设计器使用。

        我是这样解决的:

         var isInDesignMode = DesignerProperties.GetIsInDesignMode(SettingsWindow);
                if (!isInDesignMode)
                {
                    Log = new WrapperLogManager("log_wrapper.txt");
                }
        

        最后但并非最不重要的一点是,使用 ObjectDataProvider 效果不佳,只能通过 x:Static

        这几天让我完全发疯,因为绑定数据并不难;我又学到了一课!

        【讨论】:

        • 另外,我现在也有设计时预览!
        【解决方案4】:

        对于 ItemsSource,您可以使用直接 x:Static 分配,如其他答案所示,但对于 SelectedValue,您需要一个绑定,这需要一个实例来设置属性。您应该能够将静态类重构为 Singleton 以提供可绑定的实例和属性,这些实例和属性仍然可以从代码中静态引用,例如:

        public class Settings : INotifyPropertyChanged
        {
            public static Settings Instance { get; private set; }
        
            public static IEnumerable<string> Themes { get; set; }
        
            private string _lastTheme;
            public string LastTheme
            {
                get { return _lastTheme; }
                set
                {
                    if (_lastTheme == value)
                        return;
                    _lastTheme = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("LastTheme"));
                }
            }
        
            static Settings()
            {
                Themes = new ObservableCollection<string> { "One", "Two", "Three", "Four", "Five" };
                Instance = new Settings();
            }
        
            public event PropertyChangedEventHandler PropertyChanged;
        }
        

        然后 ComboBox 将使用这些绑定:

        <ComboBox ItemsSource="{x:Static local:Settings.Themes}"
        SelectedValue="{Binding Source={x:Static local:Settings.Instance}, Path=LastTheme}"   />
        

        【讨论】:

        • 谢谢大家,你们说的我都试过了,还是不行;事实上,我确实已经尝试过这条路线。当我创建一个小型测试项目时,它工作正常;由于奇怪的原因,它没有而且它太大了,所以我可以发布代码;我想我要重写这整个部分。然而,我发现通过 IntelliSense/Resharper 发生“调用目标引发异常”,我真的不知道,因为我无法进一步调试这一步。谢谢,
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-22
        • 1970-01-01
        • 2015-07-24
        相关资源
        最近更新 更多