代码之家  ›  专栏  ›  技术社区  ›  aybe

为什么这个绑定不能通过XAML工作,但可以通过代码工作?

  •  1
  • aybe  · 技术社区  · 15 年前

    我试图绑定到一个静态类的静态属性,

    它从不与以下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;
    

    你知道吗?

    4 回复  |  直到 15 年前
        1
  •  5
  •   Thomas Levesque    15 年前

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

    如果您想在XAML中执行同样的操作,那么根本不需要绑定,只需要 StaticExtension 标记扩展:

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

    local 是包含 Settings 类)

        2
  •  2
  •   Wallstreet Programmer    15 年前

    <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
  •  1
  •   aybe    15 年前

    它确实无声地抛出了一个 调用的目标引发了异常

    我正在初始化一个日志来写入一个文件;设计器最终显示了异常的详细信息,它正在visualstudio目录中创建程序文件中的文件,因此引发了一个安全异常。

    我这样修的:

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

    最后但并非最不重要的一点是,使用ObjectDataProvider从来没有那么好地工作过,只有通过x:Static

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

        4
  •  0
  •   John Bowen    15 年前

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

    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}"   />