1

我有一个以这种方式在父控件中使用的 UserControl:

<Views:TranslationTextInput  Translation="{Binding SelectedEntity.Name}"/>

父控件 DataContext 是一个包含 SelectedEntity 属性的 ViewModel。

在我的子 UserControl 中,我将一个新的 ViewModel 定义为 DataContext:

    <UserControl.DataContext>
      <vm:TranslationTextInputViewModel x:Name="vm"></vm:TranslationTextInputViewModel>
    </UserControl.DataContext>

在我后面的代码中:

    public static readonly  DependencyProperty TranslationProperty = DependencyProperty.Register("Translation", typeof(Translation),typeof(UserControl));

    // .NET Property wrapper
    public Translation Translation
    {
        get { return (Translation)GetValue(TranslationProperty); }
        set { SetValue(TranslationProperty, value); }
    }


 public TranslationTextInput(){
     InitializeComponent();
     DataContext = new TranslationTextInputViewModel();
     SetBinding(TranslationProperty,   new Binding { Path = new PropertyPath     ("Translation"), Mode = BindingMode.OneWayToSource });

执行时出现绑定错误:

System.Windows.Data Error: 40 : BindingExpression path error: 'SelectedEntity' property not found on 'object' ''TranslationTextInputViewModel' (HashCode=49954236)'. BindingExpression:Path=SelectedEntity.Name; DataItem='TranslationTextInputViewModel' (HashCode=49954236); target element is 'TranslationTextInput' (Name='InputControl'); target property is 'Translation' (type 'Translation')

似乎 SelectedEntity 是在子 UserControl 的 Viewmodel 上查找的,但应该使用父 ViewModel 的属性。我该如何解决这个问题?

编辑:

    public TranslationTextInputViewModel()
    {
        //EnglishTranslation = tranlsations["en"];
    }

    public string EnglishTranslation
    {
        get
        {
            if (!Translation.TranslationDict.ContainsKey(new CultureInfo("en").LCID))
                Translation.Translations.Add(new TranslationItem() { Text = "", Lcid = new CultureInfo("en").LCID });
            return Translation.TranslationDict[new CultureInfo("en").LCID].Text;
        }
        set
        {
            Translation.TranslationDict[new CultureInfo("en").LCID].Text = value;
        }

    }

    public string SelectedTranslation
    {
        get
        {
            if (!Translation.TranslationDict.ContainsKey(_selectedLanguage))
                Translation.Translations.Add(new TranslationItem() { Text = "", Lcid = _selectedLanguage });
            return Translation.TranslationDict[_selectedLanguage].Text;

        }

        set
        {
            Translation.TranslationDict[_selectedLanguage].Text = value;
        }
    }

    private Translation _translation;

    public Translation Translation
    {
        get
        {
            if (_translation == null)
                _translation = new Translation();
            return _translation; }
        set { _translation = value; }
    }

    private int _selectedLanguage;
    public int SelectedLanguage
    {
        get
        {
            return _selectedLanguage;
        }
    }

    public List<CultureInfo> AvailableLanguages
    {
        get
        {

            return (from x in PqsLocalization.AvailableLanguages where x.Name != "en" select x).ToList();
        }
    }

    public RelayCommand<int> LanguageChanged { get; private set; }


    private void LanguageChangedExecute(int lang)
    {
        _selectedLanguage = lang;
        RaisePropertyChanged("SelectedLanguage");
        RaisePropertyChanged("SelectedTranslation");
    }
4

4 回答 4

4

你真的不应该DataContext从. 通过这样做,您可以防止任何其他内容被传递给,这违背了 WPF 拥有独立 UI 和数据层的最大优势之一。UserControlUserControlDataContextUserControl

创建 UserControl 时,您将 设置DataContext为 new TranslationTextInputViewModel,并且TranslationTextInputViewModel没有名为 的属性SelectedEntity,因此您的绑定失败。

我的建议?不要DataContextUserControl.

如果您希望将特定 ViewModel 用于特定 UserControl,请将其添加到您的ParentViewModel并将其作为 传递DataContext,例如:

<Window.Resources>
    <DataTemplate DataType="{x:Type vm:TranslationTextInputViewModel}">
        <Views:TranslationTextInput />
    </DataTemplate>
</Window.Resources>

或这个:

<Views:TranslationTextInput 
    DataContext="{Binding SelectedTranslationTextInputViewModel}" />

或者,如果您ViewModel包含特定于UserControl自身的功能并且不应该是您的应用程序层的一部分,则将该代码放在代码隐藏中UserControl并完全摆脱ViewModel

于 2012-10-10T13:05:14.257 回答
1

试试这个:

<Views:TranslationTextInput  Translation="{Binding SelectedEntity.Name}" DataContext="{Binding DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}" /> 
于 2012-10-10T12:18:41.833 回答
1

一旦你设置了DataContext你的绑定将使用它,所以我希望这种行为——SelectedEntity.NameTranslationTextInputViewModel.

有几种方法可以让这个工作。就个人而言,我喜欢在视图模型本身(具有视图模型属性的视图模型)中对这些关系进行建模,但在这种情况下,我可能会尝试这样做,尽管感觉很不愉快:

<Views:TranslationTextInput 
    Translation="{Binding DataContext.SelectedEntity.Name,
                  RelativeSource={RelativeSource FindAncestor,
                                  AncestorType=ParentControlType}}" />
于 2012-10-10T12:22:28.470 回答
0

这是因为您在构造函数中将TranslationTextInput.DataContext设置为TranslationTextInputViewModel

于 2012-10-10T12:20:05.577 回答