2017-12-19 2 views
0

Ich habe ein paar Probleme mit meiner Custom Control - Zwei-Wege-Bindung funktioniert nicht, wenn ich es in der Vorlage verwenden. So habe ich erstellte Vorlage XAMLWPF TwoWay-Bindung funktioniert nicht mit CustomControl in Vorlage

<ResourceDictionary 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       xmlns:local="clr-namespace:WpfApp1" 
       xmlns:controls="clr-namespace:GUIControls;assembly=GUIControls" 
       > 

    <ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       xmlns:local="clr-namespace:WpfApp1" 
       xmlns:controls="clr-namespace:GUIControls;assembly=GUIControls" 
       > 

<ControlTemplate x:Key="YParamCombo" TargetType="ContentControl"> 
    <controls:ParamCombo Header="MY CONTROL TEMPLATE" 
    Items="{Binding Items}" 
    PCValue="{Binding Codes[MY_CONTROL_TEMPLATE], Mode=TwoWay}" 
    Required="True" 
    MultiSelect="False"/> 
</ControlTemplate> 

<ControlTemplate x:Key="YComboBox" TargetType="ContentControl"> 
    <ComboBox  DisplayMemberPath="Name" 
    StaysOpenOnEdit="True" 
    ItemsSource="{Binding Items}" 
    SelectedValue="{Binding Codes[STANDARD_TEMPLATE], Mode=TwoWay}" 
    SelectedValuePath="Code"/> 
</ControlTemplate>  

MainWindow.xaml

<Window x:Class="WpfApp1.MainWindow" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
xmlns:local="clr-namespace:WpfApp1" 
mc:Ignorable="d" 
xmlns:controls="clr-namespace:GUIControls;assembly=GUIControls" 
Title="MainWindow" Height="250" Width="525"> 

<Grid Margin="0,0,0,-1"> 
    <Button Margin="62,162,299,4" Content="Show Codes-1" Click="Button_Click2"></Button> 
    <StackPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch"> 
     <HeaderedContentControl Header="STANDARD CONTROL XAML" > 
      <ComboBox DisplayMemberPath="Name" 
       StaysOpenOnEdit="True" 
       ItemsSource="{Binding Items}" 
       SelectedValue="{Binding Codes[STANDARD_XAML]}" 
       SelectedValuePath="Code"/> 
     </HeaderedContentControl> 
     <HeaderedContentControl Header="STANDARD CONTROL TEMPLATE" > 
      <ContentControl Height="23" Template="{StaticResource YComboBox}"/> 
     </HeaderedContentControl> 
     <ContentControl Height="44" Template="{StaticResource YParamCombo}"> 
     </ContentControl> 
     <controls:ParamCombo Header="MY CONTROL XAML" 
      Items="{Binding Items}" 
      PCValue="{Binding Codes[MYCONTROL_XAML], Mode=TwoWay}" 
      Required="True" 
      MultiSelect="False"/> 
    </StackPanel> 
</Grid> 

cs

using System.Linq; 
using System.Windows; 

namespace WpfApp1 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      DataContext = new WModel(); 
      InitializeComponent(); 
     } 
     private WModel vm { get { return (DataContext as WModel); } } 
     private void Button_Click2(object sender, RoutedEventArgs e) 
     { 
      MessageBox.Show(string.Join(";", vm.Codes._codes.Select(x => x.Key + "=" + x.Value).ToArray())); 
     } 
    } 
} 

using GUIControls; 
using System; 
using System.Collections.Generic; 
using System.Collections.ObjectModel; 
namespace WpfApp1 
{ 
    public class WModel 
    { 
     public WModel() 
     { 
      Codes = new CodesClass(); 
     } 
     public string Caption { get; set; } 
     public ObservableCollection<Dict> Items 
     { 
      get 
      { 
       return new ObservableCollection<Dict>() 
       { 
        new Dict(){ Name = "Name1", Code = "Code1" } , 
        new Dict(){ Name = "Name2", Code = "Code2" } 
       }; 
      } 
     } 
     public CodesClass Codes { get; set; } 
    } 
    public class Dict : IDict 
    { 
     public string Name { get; set; } 
     public string Code { get; set; } 
    } 
    public class CodesClass 
    { 
     public Dictionary<string, object> _codes; 
     public CodesClass() 
     { 
      _codes = new Dictionary<string, object>(); 
     } 
     public object this[string param] 
     { 
      get 
      { 
       if (_codes.ContainsKey(param)) 
        return _codes[param]; 
       else 
        return null;// "I have no " + param; 
      } 
      set 
      { 
       _codes[param] = value; 
      } 
     } 
    } 
} 

Wenn ich App ausführen und wählen Sie alle 4 c omboboxes und Drücken Sie die Taste, das kann ich twoway sehen in einer Combobox Bindung (Custom Control in Vorlage deklariert) funktionieren nicht

--------------------------- 

--------------------------- 
STANDARD_XAML=Code2;STANDARD_TEMPLATE=Code2;MYCONTROL_XAML=Code2 
--------------------------- 
ОК 
--------------------------- 

Einige Code von Steuer

 public static readonly DependencyProperty PCValueProperty = 
     DependencyProperty.Register("PCValue", typeof(string), typeof(ParamCombo), 
      new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, new PropertyChangedCallback(OnPCValuePropertyChanged))); 
    //new PropertyMetadata(new PropertyChangedCallback(OnValuePropertyChanged)));, new PropertyChangedCallback(OnPCValuePropertyChanged)) 
    #endregion 

    private static void OnPCValuePropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) 
    { 
     ParamCombo paramCombo = (ParamCombo)sender; 
     paramCombo.UpdateSelected((e.NewValue == null) ? "" : e.NewValue.ToString()); 
    } 

Vielen Dank für Ihre Hilfe!

+1

Es gibt keine Notwendigkeit, ** schrei ** (fett buchstaben = schreien), aber INotifyPropertyChanged für eine funktionierende Bindung zu implementieren –

+0

Sorry, ich habe es nicht fett gemacht Ich kopierte nur Text des Meldungsfenster. –

+0

@IlyaFetisov Ich reparierte es für Sie. Bitte reparieren Sie es beim nächsten Mal selbst. –

Antwort

0

Ich habe Probleme mit twoway Bindung in einer Combo in einer Custom Vorlage hatte und die Lösung wurde OnApplyTemplate in dem benutzerdefinierten Steuerelement außer Kraft zu setzen, Template.FindName mit der Combo zu bekommen, das DataContex Ziel der Combo erhalten und erhöhen Property in das DataContext-Objekt für die gebundene Eigenschaft. Mein Problem bestand darin, die Combo zu aktualisieren, wenn das Fenster geladen wurde.