これは、MVVMの使用に関するベストプラクティスのアドバイスに関するものです。
要素がドロップインされたときに、ラップパネルにデータを入力する必要があります。要素は均一ではなく、ラベルまたはテキストボックスのいずれかになります。パラメータの値に応じて、追加される要素は異なります。
私はこれをコードビハインドで行いました。現在、すべてをMVVMモデルに移行する過程にあり、MVVMコアプリンシパルに影響を与えることなくこれを行う方法に固執しています。このコードには、密接に関連するUI要素とロジックコンテンツの両方が含まれています。そして、私は2つをスイートMVVMに分離することができなくなりました。
VMでUI要素を作成し、UIElementタイプのObservableCollectionにデータを入力し、それをitemssourceプロパティにバインドしてみました(その後、wrappanelをリストビューに変更して全体的に有効にしました)。しかし、要素をバインドすると、コードがどのUIelementを理解できるかわからないため、これはうまくいきませんでした。
以下に投稿されているのは、私が分離する必要のあるコードのセクションです。
private void CreateVisulaQueryContent() {
VisualQueryObject visualQueryData = new VisualQueryObject();
VisualQueryObject helperVisualQueryObject = DraggedData as VisualQueryObject;
//***Taking a copy of the static DraggedData object to be bound
visualQueryData.ColumnDiscriptor = helperVisualQueryObject.ColumnDiscriptor;
visualQueryData.ComparedValue = helperVisualQueryObject.ComparedValue;
visualQueryData.JoinWithColumnDescriptor = helperVisualQueryObject.JoinWithColumnDescriptor;
visualQueryData.LabelType = helperVisualQueryObject.LabelType;
visualQueryData.OperatorValue = helperVisualQueryObject.OperatorValue;
if (visualQueryData.LabelType == "column")
{
ColumnDescriptionObject descriptionValue = visualQueryData.ColumnDiscriptor;
Label droppedElement = new Label();
Binding binding = new Binding();
binding.Source = visualQueryData;
binding.Path = new PropertyPath("ColumnDiscriptor");
binding.Mode = BindingMode.TwoWay;
droppedElement.SetBinding(Label.DataContextProperty, binding);
droppedElement.Content = visualQueryData.ColumnDiscriptor.TableName + "." + visualQueryData.ColumnDiscriptor.ColumnName;
droppedElement.Foreground = Brushes.White;
droppedElement.Background = Brushes.DarkOrange;
droppedElement.BorderThickness = new Thickness(5);
droppedLabel.MouseDoubleClick += columnLabel_MouseDown;
ViewUIElements.Add(droppedElement);
}
else if (visualQueryData.LabelType == "controller")
{
Label droppedElement = new Label();
Binding binding = new Binding();
binding.Source = visualQueryData;
binding.Path = new PropertyPath("OperatorValue");
binding.Mode = BindingMode.TwoWay;
droppedElement.SetBinding(Label.DataContextProperty, binding);
droppedElement.Content = draggedContent.OperatorValue;
droppedElement.Foreground = Brushes.White;
droppedElement.Background = Brushes.Crimson;
droppedElement.BorderThickness = new Thickness(5);
droppedElement.MouseDoubleClick += columnLabel_MouseDown;
ViewUIElements.Add(new Label());
}
else if (visualQueryData.LabelType == "value")
{
TextBox droppedElement = new TextBox();
Binding binding = new Binding();
binding.Source = visualQueryData;
binding.Path = new PropertyPath("ComparedValue");
binding.Mode = BindingMode.TwoWay;
droppedElement.SetBinding(TextBox.TextProperty, binding);
droppedElement.MouseDoubleClick += columnLabel_MouseDown;
ViewUIElements.Add(droppedElement);
}
QueryDesignerModel.QueryDesignHelperCollection.Add(visualQueryData);
}
どんな助けでも深く感謝します!