我正在爲WYSIWYG HTML編輯器控制Ribbon
控件。功能區具有您期望看到的典型Bold,Italic,Underline,FontFamily等控件。我將專注於此示例的Bold功能。WPF ::自定義控件的依賴項屬性不起作用
我想Ribbon
是可重複使用的,所以我添加了一個依賴項屬性(DP)和相關屬性包裝到控件的代碼後面(標準的樣板東西):
public partial class EditorRibbon: UserControl
{
public static readonly DependencyProperty IsBoldProperty =
DependencyProperty.Register(
"IsBold",
typeof (bool),
typeof (EditorRibbon),
new PropertyMetadata(default(bool)));
public bool IsBold
{
get { return (bool) GetValue(IsBoldProperty); }
set { SetValue(IsBoldProperty, value); }
}
}
...和在XAML我有我RibbonToggleButton
,我已經綁定的IsChecked
屬性依賴屬性:
<UserControl x:Class="My.EditorRibbon">
<r:RibbonToggleButton Command="ToggleBold"
ToolTip="{Binding RelativeSource={RelativeSource Self}, Path=Command.Text}"
SmallImageSource="{StaticResource ToggleBoldIcon}"
IsChecked="{Binding IsBold}" />
</UserControl>
在我Editor
窗口,我已經綁定的0123的IsBold
財產對窗口的視圖模型的常規屬性:
<Window x:class="My.MainWindow>
<My.EditorRibbon IsBold="{Binding SelectionIsBold}"/>
</Window>
這裏是SelectionIsBold
屬性:
public bool SelectionIsBold
{
get { return _selection.IsBold(); }
}
...我提高NotifyPropertyChanged()
事件(在主窗口的視圖模型),只要在選擇RichTextBox
變化:
public class MainWindowViewModel : BaseViewModel
{
public MainWindowViewModel(MainWindow window)
{
rtb.SelectionChanged += rtb_OnSelectionChanged;
}
private void rtb_OnSelectionChanged(object sender, RoutedEventArgs routedEventArgs)
{
NotifyPropertyChanged(()=>SelectionIsBold);
}
}
在我看來,這應該是足以改變IsChecked
統計每當選擇發生變化時,RibbonToggleButton
的電子郵件都會被刪除,但事實並非如此。儘管改變了選擇,並且儘管NotifyPropertyChanged()
如預期的那樣射擊,SelectionIsBold
屬性的斷點(是的,我已經取消選擇VS的「Step Over Property」設置)從未被擊中。某處,刷新該值的請求無法正確傳播。
在setter中設置值之後,是否需要在IsBold屬性上觸發NotifyPropertyChanged()?
難道你不需要綁定你的命令嗎? Command =「{Binding ToggleBold}」 –
其中是'rtb_OnSelectionChanged' – Neil
@Neil - rtb_OnSelectionChanged在包含RichTextBox和EditorRibbon的窗口中。 –