2010-06-08 17 views
1

我想绑定到静态类的静态属性, 此属性包含从文件反序列化的设置。为什么此绑定不能通过XAML工作,而是通过代码实现?

它从来没有与下面的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; 

任何想法?

谢谢:-)

回答

1

我找到了答案!

它并默默地抛出一个异常已通过调用我不知道更多的目标扔...

我初始化写入到文件中的记录;设计师终于展示了异常的细节,它正在寻找在Program Files中的Visual Studio目录中创建文件,因此引发了安全异常。

显然VS将该文件复制到其文件夹中,以供其Designer使用。

我固定它是这样的:

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

最后但并非最不重要的,使用的ObjectDataProvider从来没有当过好,只有通过X:静态

这是推动我完全疯了几天,因为它是没有那么难绑定数据;我刚刚学到了另一课!

+0

另外,我现在也有设计时预览! – Aybe 2010-06-10 10:55:59

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"; } } 
    } 
} 
5

代码隐藏不进行装订,它直接源分配给ComboBox ...

如果您想在XAML中执行相同的操作,则根本不需要绑定,y欧刚需StaticExtension标记扩展:

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

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

0

对于ItemsSource时,你可以使用直接X:静态分配如图所示其他答案,但是对于SelectedValue,你需要一个Binding,它需要一个实例来设置一个属性。你应该能够重组静态类为单身提供了可绑定实例和属性,它仍然可以从代码中静态引用的,是这样的:

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 ItemsSource="{x:Static local:Settings.Themes}" 
SelectedValue="{Binding Source={x:Static local:Settings.Instance}, Path=LastTheme}" /> 
+0

谢谢大家,我试过了你说的一切,它不起作用;实际上我确实已经尝试过这条路线。 当我创建一个小测试项目时,它工作正常;由于奇怪的原因在这一个它不,它太大,所以我可以发布的代码;我想我会重写这整个部分。 然而,我发现“通过调用的目标引发异常”是通过IntelliSense/Resharper发生的,我不知道,因为我无法进一步调试此步骤。 谢谢, – Aybe 2010-06-09 15:28:05

相关问题