背景
我所拥有的摘要是一个用户控件
MarkdownEditor
. 它包含一个
TextBox
显示属性如下
FontFamily
,
Background
等由绑定控制。它从一个
MarkdownEditorOptions
类,其中包含这些值的每个属性。我的代码如下
在
ShellView
,显示如何设置
文本框
在我的
标记编辑器
<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 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}}" />
这个
标记编辑器选项
班
public class MarkdownEditorOptions : ObservableObject
{
protected FontFamily _fontFamily;
protected int _fontSize;
protected FontWeight _fontWeight;
protected Color _background;
protected Color _foreground;
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中的“我的文本框”始终显示的是
标记编辑器选项
. 在我展示的简单XAML中,似乎没有应用红色背景。怎么了?
[更新时间:11月17日下午4:25]
一些想法
我想这和
Path=Options.FontSize
. 也许这个绑定将跟踪
选项
而不是
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">
我试着用和不绑。似乎是二传手
标记编辑器选项
如我所说,是运行
Debug.WriteLine()
但背景等没有更新。
<DataTemplate DataType="{x:Type vm:EditorTabViewModel}">
<me:MarkdownEditor>
<me:MarkdownEditor.Options>
<me:MarkdownEditorOptions Background="Red" />
</me:MarkdownEditor.Options>
</me:MarkdownEditor>
</DataTemplate>