Workflow foundation 4 在对话框中使用时,如何配置ExpressionTextBox绑定/OwnerActivity?
我们的团队正在围绕我们的电子邮件活动开发定制活动设计器。这是一个非常直接的设计器,允许用户输入设置/凭据,但是我们没有将所有可设置的选项混杂在活动设计器中,而是考虑在对话框窗口中放置一些设置。(单击服务器地址框旁边的按钮时打开) 我们的一些电子邮件活动属性是InArguments,因此我们尝试使用ExpressionTextBox来显示这些值,但运气不好。主要问题是我们不确定如何在ExpressionTextBox上正确设置绑定和OwnerActivity。在Activity Designer的xaml中,只需使用InArgument的转换器设置Expression=ModelItem.Property并设置OwnerActivity=ModelItem即可,如下所示:Workflow foundation 4 在对话框中使用时,如何配置ExpressionTextBox绑定/OwnerActivity?,workflow-foundation-4,Workflow Foundation 4,我们的团队正在围绕我们的电子邮件活动开发定制活动设计器。这是一个非常直接的设计器,允许用户输入设置/凭据,但是我们没有将所有可设置的选项混杂在活动设计器中,而是考虑在对话框窗口中放置一些设置。(单击服务器地址框旁边的按钮时打开) 我们的一些电子邮件活动属性是InArguments,因此我们尝试使用ExpressionTextBox来显示这些值,但运气不好。主要问题是我们不确定如何在ExpressionTextBox上正确设置绑定和OwnerActivity。在Activity Designer
<view:ExpressionTextBox HintText="Enter a VB Expression" Expression="{Binding ModelItem.ServerAddress, ConverterParameter=In, Converter={StaticResource ArgumentToExpressionConverter}, Mode=TwoWay}" ExpressionType="{x:Type system:String}" OwnerActivity="{Binding ModelItem}" Margin="2" MaxLines="1" />
如果有人对我们如何在对话中实现这一点有任何想法,请提供建议 其实,这更像是一个WPF\MVVM问题,而不是WF4 在开发自定义活动设计器时,您只需记住一件事:对designer\dialog所做的任何更改都应通过XAML绑定表达式或
ModelItem.Properties上的代码反映在ModelItem
上
现在,您何时以及如何做到这一点,有几个答案,但这实际上是一个实现细节,取决于您希望如何做到这一点
假设您正在服务器地址框单击旁边显示“打开”按钮对话框。我们还假设您可以通过对话框的名称访问它们。此时,您可以访问ModelItem
,因此只需根据需要设置其属性:
private void ButtonNextToServerAddressBox_OnClick(object sender, RoutedEventArgs e)
{
var dialog = new ServerAddressEditor();
var result = dialog.ShowDialog();
if (result ?? false)
{
ModelItem.Properties["Server"].SetValue(new InArgument<string>(dialog.ServerTextBox.Text));
ModelItem.Properties["Port"].SetValue(new InArgument<string>(dialog.PortTextBox.Text));
// ... set all other properties
}
}
private void按钮nexttoserveraddressbox\u OnClick(对象发送方,RoutedEventArgs e)
{
var dialog=newserveraddresseditor();
var result=dialog.ShowDialog();
如果(结果??错误)
{
modeleItem.Properties[“Server”].SetValue(新的inarument(dialog.ServerTextBox.Text));
modeleItem.Properties[“Port”].SetValue(新的inarument(dialog.PortTextBox.Text));
//…设置所有其他属性
}
}
现在,如果您正在使用任何其他模式,或者您想要纯MVVM,那么由于ModelItem的工作方式,它可能会有点棘手。但是这是一个非常好的方法。我通过在对话框的ViewModel中创建一个属性来保存活动设计器的ModelItem,从而解决了这个问题
public ModelItem OwnerActivity {
get { return _OwnerActivity; }
set { _OwnerActivity = value; }
}
vm.OwnerActivity = this.DataContext.ModelItem;
然后,我将对话框中表达式文本框的Xaml设置为绑定到:
<view:ExpressionTextBox HintText="Enter a VB Expression" Expression="
{Binding Path=OwnerActivity.ServerAddress, ConverterParameter=In, Converter=
{StaticResource ArgumentToExpressionConverter}, Mode=TwoWay}" ExpressionType="
{x:Type system:String}" OwnerActivity="{Binding OwnerActivity}" Margin="2"
MaxLines="1" />
我们知道如何设置ModelItem的属性。这不是问题所在。问题是当ExpressionTextbox未直接用于活动设计器时,如何(如果可能)设置其绑定。
// declare a ModelEditingScope to make changes transactional
private ModelEditingScope _editScope;
// add this to the constructor of the dialog to begin transactional edits on the ModelItem
_editScope = editorViewModel.OwnerActivity.BeginEdit();
// ok & cancel button click event to commit or revert the changes.
private void OK_Click(object sender, RoutedEventArgs e)
{
_editScope.Complete();
this.DialogResult = DialogResult.OK;
this.Close();
}
private void Cancel_Click(object sender, RoutedEventArgs e)
{
_editScope.Revert();
this.DialogResult = DialogResult.Cancel;
this.Close()
}