2010-11-20 31 views
1

我有綁定一個自定義的控制像下面綁定設置屬性但UI不更新。我可以在引用的項目/控制內進行調試嗎

<DataTemplate DataType="{x:Type vm:EditorTabViewModel}"> 
    <me:MarkdownEditor 
     Options="{Binding Path=Options, RelativeSource={RelativeSource AncestorType=Window}}" /> 
</DataTemplate> 

我發現綁定(Window1.Options)被置位(通過代碼在調試模式下步進後),降價編輯器選項(應該設置字體,顏色等)沒有設置,或者至少UI不更新。我想bug在MarkdownEditor.xaml.cs發生了什麼,但那是另一個(引用)的項目。我如何確認至少設置了MarkdownEditor.Options

其實我已經測試的MarkdownEditor側通過以​​下

<Window ...> 
    <Grid> 
     <Button Content="Options" Click="Button_Click" Grid.Row="0" /> 
     <me:MarkdownEditor Options="{Binding Options, RelativeSource={RelativeSource AncestorType=Window}}" Grid.Row="1" /> 
    </Grid> 
</Window> 

工作,所以不同的是,後者僅僅是在一個Window一個Grid一個MarkdownEditor。在一個失敗是必然的ObservableCollection<TabViewModel>

Visual Studio解決方案一TabControl複製問題

MarkdownEditor我沒有解釋的事情真的很不錯,所以一個簡單的項目我做了減去所有不必要的噪音上傳到media fire所以你可以看看什麼是錯

顯示問題的視頻Screenr

只需一個簡單的用法,編輯窗口/網格。

在綁定到ObservableCollection<EditorTabViewModel>TabControl一起使用時的結合工程確定

然後,如在2個TextBox ES更新其值所示的結合作品。但編輯不更新

回答

3

閱讀肯特Boogaart的回答this問題後,我認爲,要改變的SetValue到SetCurrentValue正確的地方是不是在CLR屬性,但在構造函數MarkDownEditor。

public MarkdownEditor() 
{ 
    InitializeComponent(); 
    //Options = new MarkdownEditorOptions(); 
    this.SetCurrentValue(OptionsProperty, new MarkdownEditorOptions()); 
    DataContext = this; 
} 

事實上,如果沒有這個,SetSurrentValue也會起作用,因爲Options會通過綁定來設置。

要確認您的綁定實際上已被SetValue覆蓋,您可以在TabUsage的某些事件中添加此代碼(例如FontSize TextBox的PreviewMouseRightButtonDown),並且綁定將再次開始工作。

private void TextBox_PreviewMouseRightButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e) 
{ 
    MarkdownEditor.MarkdownEditor editor = VisualTreeHelpers.GetVisualChild<MarkdownEditor.MarkdownEditor>(this); 
    Binding binding = new Binding(); 
    binding.Path = new PropertyPath("Options"); 
    binding.Source = this; 
    binding.Mode = BindingMode.TwoWay; 
    editor.SetBinding(MarkdownEditor.MarkdownEditor.OptionsProperty, binding); 
} 
+0

YAY,它的工作原理!你很棒@Meleak,從你身上學到很多東西!謝謝 – 2010-11-21 10:00:27

+0

HBut我仍然沒有真正得到'SetCurrentValue()'...我想時間來重新閱讀和消化的另一個問題... – 2010-11-21 10:05:55

+0

@jiewmeng:呵呵肯定的事情:)尼斯控制你正在btw工作 – 2010-11-21 22:06:59

相關問題