为什么绑定不更新

时间:2010-11-17 08:19:50

标签: c# wpf data-binding

背景

我所拥有的摘要是UserControl MarkdownEditor。它包含TextBox,其显示属性如FontFamilyBackground等由Bindings控制。它从MarkdownEditorOptions类中获取这些值,该类包含每个值的属性。我的代码如下所示

ShellView 中,展示如何为TextBox

中的MarkdownEditor设置显示选项
<me:MarkdownEditor>
    <me:MarkdownEditor.Options>
        <me:MarkdownEditorOptions Background="Red" />
    </me:MarkdownEditor.Options>
</me:MarkdownEditor>

MarkdownEditor.xaml.cs ,MarkdownEditor的DataContext(UserControl),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与选项值绑定的方式

<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

// 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中的My TextBox始终显示MarkdownEditorOptions构造函数的默认值。在我所示的简单XAML中,似乎没有应用红色背景。怎么了?

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

一些想法

我认为这与Path=Options.FontSize有关。也许此绑定会跟踪Options而不是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">

我尝试使用和不使用绑定。似乎MarkdownEditorOptions中的setter正在运行,因为我添加了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 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

这已在我在StackOverflow上的另一个问题中修复:Binding Setting Property but UI not updating. Can I debug within referenced project/control?