【问题标题】:WPF Binding HelpWPF 绑定帮助
【发布时间】:2010-11-17 17:43:51
【问题描述】:

我没有太多使用 WPF,所以解决这个问题可能很简单。

在我正在开发的 ide 中,它将有多个控件(文本编辑器),每个控件都托管在一个选项卡中,就像 VS 对每个源文件所做的那样。当用户单击 new 时,“主机”会创建一个新的 EditorWindow(一个用户控件),创建一个新选项卡,并告诉该选项卡显示它创建的 EditorWindow,然后将一个名为 currentWindow(类型为 EditorWindow)的属性更新为目前活跃。 EditorWindow 内部是文本编辑器,其名称为 textEditor(也是一个属性)。我要做的是从我正在使用的文本编辑器控件的快速启动源中获取此代码

                    <StackPanel>
                        <CheckBox Checked="EditiorOptionsChecked" IsChecked="{Binding ElementName=Control, Path=currentWindow.textEditor.IsIndicatorMarginVisible}" Content="Indicator margin visible" />
                        <CheckBox Checked="EditiorOptionsChecked" IsChecked="{Binding ElementName=Control, Path=currentWindow.textEditor.IsLineNumberMarginVisible}" Content="Line number margin visible" />
                        <CheckBox Checked="EditiorOptionsChecked" IsChecked="{Binding ElementName=Control, Path=currentWindow.textEditor.IsRulerMarginVisible}" Content="Ruler margin visible (useful for fixed-width fonts only)" />
                        <CheckBox Checked="EditiorOptionsChecked" IsChecked="{Binding ElementName=Control, Path=currentWindow.textEditor.IsSelectionMarginVisible}" Content="Selection margin visible" />
                    </StackPanel>

将其放入主机控件 xaml 中,并将复选框绑定到语法编辑器。我尝试了几种不同的方法都无济于事。 Control 是托管所有选项卡的窗口的名称,而 path 显然也应该是复选框绑定的属性。我很确定问题是在初始运行时 currentWindow 没有初始化,因此我的绑定永远不会更新,但我不知道如何解决这个问题。谢谢!

【问题讨论】:

    标签: wpf data-binding binding


    【解决方案1】:

    由于您是 WPF 新手,您可能不知道属性必须实现某种更改通知才能使绑定起作用。例如,如果路径“currentWindow.textEditor.IsIndicatorMarginVisible”中的任何属性发生更改,您需要通知绑定引擎它已更改。如果您将这些属性实现为 DependencyPropertys,则更改跟踪是免费的。否则,您应该实现 INotifyPropertyChanged。

    【讨论】:

      【解决方案2】:

      我发现Snoop 实用程序是进行快速绑定调试的最简单方法,您应该尝试使用它,看看它是否告诉您绑定属性的任何有用信息。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-10-13
        • 1970-01-01
        • 2011-09-28
        • 1970-01-01
        • 1970-01-01
        • 2011-03-16
        • 2012-11-18
        • 1970-01-01
        相关资源
        最近更新 更多