Generic.xaml绑定到objectDataProvider

时间:2017-04-24 09:21:20

标签: c# wpf xaml

我不知道问题是什么,因为我几乎可以肯定我做了其他帖子告诉我的事情。我已经将一个可观察的字符串集合绑定到一个comboBox,所以它应该可以工作。

数据类:

namespace UIBlocksLib.Data_VM__classes
{
  public class BlockController : INotify, IStatementCollection
   {
      private List<UIStackBlock> _mUIStackBlocks = new List<UIStackBlock>();
      public ObservableCollection<string> _mUIVariables = new ObservableCollection<string>() { "VariableA", "VariableB", "VariableC" };

      public event PropertyChangedEventHandler PropertyChanged;

      public ObservableCollection<string> getVariables
      {
         get
         {
            return _mUIVariables;
         }
         set
         {
            _mUIVariables = value;
            onPropertyChanged("_mUIVariables");
         }
      }

      public BlockController()
      {
      }
      public void addVariable(string aVariableName)
      {
         _mUIVariables.Add(aVariableName);
         onPropertyChanged("_mUIVariables");
      }

      public void addUIStackBlock(UIStackBlock aUIStackBlock)
      {
         throw new NotImplementedException();
      }

      public void onPropertyChanged(string aPropertyName)
      {
         PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(aPropertyName));
      }

      public void removeStackBlockByIndex(int aIndex)
      {
         throw new NotImplementedException();
          }
       }
    }

我的generic.xaml中的objectDataProvider

 <ObjectDataProvider x:Key="dataObject"
                             ObjectType="{x:Type dataClass:BlockController}"
                             MethodName="getVariables">
    </ObjectDataProvider>

和我的风格绑定到我的班级

<Style TargetType="{x:Type local:UISet}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate  TargetType="{x:Type local:UISet}">
                    <Grid>
                        <Rectangle MouseLeftButtonDown="MouseLeftButtonDown" Height="{TemplateBinding Height}" Width="{TemplateBinding Width}" Fill="#AD2B27" ClipToBounds="True"/>
                        <ComboBox DataContext="{staticResource dataObject}" Background="#FFEE4A4A" x:Name="comboBox" MinHeight="30" MinWidth="70" Margin="5, 23" ItemsSource="{Binding}"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

还尝试使上下文静态/不使用上下文/使用变量的名称。在查看建议时,编译器会识别getVariables。

1 个答案:

答案 0 :(得分:1)

在您的代码中,getVariables不是方法,而是属性。这样它应该有效:

 <ObjectDataProvider x:Key="dataObject"
                             ObjectType="{x:Type dataClass:BlockController}"
                             MethodName="GetVariables">
    </ObjectDataProvider>

public ObservableCollection<string> GetVariables()
        {
            return getVariables;
        }