【问题标题】:Why is Binding not updating为什么绑定不更新
【发布时间】:2010-11-17 08:19:50
【问题描述】:

背景

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

ShellView 中,展示了如何在我的MarkdownEditor 中为TextBox 设置显示选项

<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 绑定到选项值的方式

<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 中的 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&lt;TabViewModel&gt;,失败了

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

我尝试了使用和不使用绑定。 MarkdownEditorOptions 中的设置器似乎已运行,因为我添加了 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>

【问题讨论】:

    标签: c# wpf data-binding


    【解决方案1】:

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

    【讨论】:

    • 嗯,我在输出窗口中没有看到任何错误,这是我应该看的地方吗?事实上,我看到我的设置器正在运行,因为我向它们添加了Debug.WriteLine
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-05
      • 1970-01-01
      • 1970-01-01
      • 2020-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-02
      相关资源
      最近更新 更多