2010-06-08 19 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

相關問題