代码之家  ›  专栏  ›  技术社区  ›  Jiew Meng

为什么绑定没有更新

  •  1
  • Jiew Meng  · 技术社区  · 14 年前

    背景

    我所拥有的摘要是一个用户控件 MarkdownEditor . 它包含一个 TextBox 显示属性如下 FontFamily , Background 等由绑定控制。它从一个 MarkdownEditorOptions 类,其中包含这些值的每个属性。我的代码如下

    ShellView ,显示如何设置 文本框 在我的 标记编辑器

    <me:MarkdownEditor>
        <me:MarkdownEditor.Options>
            <me:MarkdownEditorOptions Background="Red" />
        </me:MarkdownEditor.Options>
    </me:MarkdownEditor>
    

    MarkdownEditor.xaml.cs ,markdowneditor(usercontrol)的dataContext,的声明 Options

    public MarkdownEditorOptions Options
    {
        get { return (MarkdownEditorOptions)GetValue(OptionsProperty); }
        set { SetValue(OptionsProperty, value); }
    }
    
    public static readonly DependencyProperty OptionsProperty =
        DependencyProperty.Register("Options", typeof(MarkdownEditorOptions), typeof(MarkdownEditor), new UIPropertyMetadata(new MarkdownEditorOptions()));
    

    MarkdownEditor.xaml :显示路线 文本框 绑定到选项值

    <TextBox Grid.Row="1" x:Name="txtEditor" AcceptsReturn="True" Text="{Binding Path=Content, UpdateSourceTrigger=PropertyChanged}" 
                FontFamily="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:MarkdownEditor}}, Path=Options.FontFamily}"
                FontSize="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:MarkdownEditor}}, Path=Options.FontSize}"
                FontWeight="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:MarkdownEditor}}, Path=Options.FontWeight}"
                Background="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:MarkdownEditor}}, Path=Options.Background, Converter={StaticResource colorToBrushConverter}}"
                Foreground="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:MarkdownEditor}}, Path=Options.Foreground, Converter={StaticResource colorToBrushConverter}}" />
    

    这个 标记编辑器选项

    // MarkdownEditorOptions
    public class MarkdownEditorOptions : ObservableObject
    {
        protected FontFamily _fontFamily;
        protected int _fontSize;
        protected FontWeight _fontWeight;
        protected Color _background;
        protected Color _foreground;
    
        // Constructor, for default options
        public MarkdownEditorOptions()
        {
            _fontFamily = new FontFamily("Consolas");
            _fontSize = 14;
            _fontWeight = FontWeights.Bold;
            _background = new Color { R = 32, G = 32, B = 32, A = 255 };
            _foreground = new Color { R = 255, G = 255, B = 255, A = 255 };
        }
    
        public FontFamily FontFamily {
            get { return _fontFamily; }
            set {
                _fontFamily = value;
                RaisePropertyChanged("FontFamily");
            }
        }
    
        public int FontSize
        {
            get { return _fontSize; }
            set {
                _fontSize = value;
                RaisePropertyChanged("FontSize");
            }
        }
    
        public FontWeight FontWeight
        {
            get { return _fontWeight; }
            set {
                _fontWeight = value;
                RaisePropertyChanged("FontWeight");
            }
        }
    
        public Color Background
        {
            get { return _background; }
            set {
                _background = value;
                RaisePropertyChanged("Background");
            }
        }
    
        public Color Foreground
        {
            get { return _foreground; }
            set {
                _foreground = value;
                RaisePropertyChanged("Foreground");
    
            }
        }
    }
    

    问题

    markdowneditor中的“我的文本框”始终显示的是 标记编辑器选项 . 在我展示的简单XAML中,似乎没有应用红色背景。怎么了?

    [更新时间:11月17日下午4:25]

    一些想法

    我想这和 Path=Options.FontSize . 也许这个绑定将跟踪 选项 而不是 Options.FontSize ?

    更新时间:11月19日

    一些观察:如果我在单独的简单窗口中使用控件

    <Window ...>
        <Window.Resources>
            <me:MarkdownEditorOptions FontFamily="Arial" FontWeight="Normal" Background="Red" x:Key="options" />
        </Window.Resources>
        <Grid>
            <Grid.RowDefinitions ... />
            <Button Content="Options ..." Grid.Row="0" Click="Button_Click" />
            <me:MarkdownEditor Grid.Row="1" Options="{StaticResource options}" x:Name="markdownEditor" />
        </Grid>
    </Window>
    

    如果我在更复杂的设置中使用它,一切都会正常工作。 TabControl 绑定到 ObservableCollection<TabViewModel> ,失败了

    <TabControl ... ItemsSource="{Binding TabsViewSource}" IsSynchronizedWithCurrentItem="True">
    

    我试着用和不绑。似乎是二传手 标记编辑器选项 如我所说,是运行 Debug.WriteLine() 但背景等没有更新。

    <DataTemplate DataType="{x:Type vm:EditorTabViewModel}">
        <!--<me:MarkdownEditor Options="{Binding RelativeSource={RelativeSource AncestorType={x:Type v:ShellView}}, Path=ViewModel.Options}" />-->
        <me:MarkdownEditor>
            <me:MarkdownEditor.Options>
                <me:MarkdownEditorOptions Background="Red" />
            </me:MarkdownEditor.Options>
        </me:MarkdownEditor>
    </DataTemplate>
    
    2 回复  |  直到 14 年前
        1
  •  2
  •   ike3    14 年前

    通常这是由于路径不正确造成的。不幸的是,它不会引发异常,您可以尝试将vs调试程序附加到应用程序,并检查调试日志中是否有任何绑定错误。

        2
  •  0
  •   Community CDub    8 年前