简体   繁体   中英

Why does this binding doesn't work through XAML but does by code?

I am trying to bind to a static property on a static class, this property contains settings that are deserialized from a file.

It never works with the following 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}"   />

It does work by code however :

comboboxThemes.ItemsSource = Settings.Themes;

Any idea ?

Thank you :-)

Your code-behind doesn't perform a binding, it directly assigns a source to the ComboBox ...

If you want to do the same in XAML, you don't need a binding at all, you just need the StaticExtension markup extension :

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

(where local is the xmlns mapping for the namespace containing the Settings class)

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>

Code behind:

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

I have found the answers !

It did silently throw an Exception has been thrown by the target of an invocation i didn't know more ...

I was initializing a log that writes to a file; the designer finally showed up the details of the exception, it was looking for creating the file in Visual Studio directory which is in Program Files, hence a security exception was thrown.

Apparently VS copies the file to its folder, for its Designer.

I fixed it like this :

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

Last but not least, using ObjectDataProvider never worked as well, only through x:Static

This was driving me totally crazy for a few days as it's not so hard to bind data; I just learned another lesson !

For the ItemsSource you can use a direct x:Static assignment as shown in the other answers but for the SelectedValue you need a Binding, which requires an instance on which to set a property. You should be able to restructure the static class into a Singleton to provide a bindable instance and property which can still be referenced statically from code, something like:

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

Then the ComboBox would use these bindings:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM