简单的新手问题。
我有一个按钮,当前在视图模型中执行"插入"代码:
<Button Content="Insert" Grid.Column="3" Grid.Row="2" Height="75"
Command="{Binding Insert}" />
在同一个用户控件中,我有一个自定义控件 CustomInkCanvas,定义为:
<wc:CustomInkCanvas x:Name="myInkCanvas"
Vocabulary="{Binding Vocabulary, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}"
Text="{Binding Text, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
WordPad="{Binding WordPad, UpdateSourceTrigger=PropertyChanged, Mode=OneWayToSource}"
CloseCharacterPads ="{Binding CloseCharacterPads}"
EditWord ="{Binding EditWord}"
/>
我想做的是添加一些内容,如下所示:
FormatText = {Binding ??? ElementName=Insert ???}
到自定义控件 XAML,以便单击按钮将向自定义控件发送一条消息以重新格式化自身。此外,此重新格式化需要在按钮连接到的视图模型的"插入"方法之前完成。 为了清楚起见,我需要单击按钮以首先告诉自定义墨水画布重新格式化,然后再在视图模型中执行活动。
这是否可以通过 XAML 完成,如果是,如何完成?
提前感谢您对此的任何帮助。(如果我有一个强项,XAML不是它!
您可以使用 2 个属性来执行此操作,一个在视图模型上,一个在自定义控件上
自定义墨水画布.cs
public string FormatText
{
get { return (string)GetValue(FormatTextProperty); }
set { SetValue(FormatTextProperty, value); }
}
// Using a DependencyProperty as the backing store for FormatText. This enables animation, styling, binding, etc...
public static readonly DependencyProperty FormatTextProperty =
DependencyProperty.Register("FormatText", typeof(string),
typeof(CustomInkCanvas), new PropertyMetadata(string.Empty));
在你的视图模型中(类需要实现INotifyPropertyChanged)
private string _updatedText = string.Empty;
public string UpdatedText
{
get { return _updatedText ; }
set
{
_updatedText = value;
OnPropertyChnaged("UpdatedText");
}
}
然后在自定义控件上
<wc:CustomInkCanvas x:Name="myInkCanvas"
FormatText={Binding Path="UpdatedText"} />
在您的命令中插入
this.UpdatedText = "your text";
//your insert code