Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 在DataTemplate中更新DataTemplate的绑定_Wpf_Data Binding - Fatal编程技术网

Wpf 在DataTemplate中更新DataTemplate的绑定

Wpf 在DataTemplate中更新DataTemplate的绑定,wpf,data-binding,Wpf,Data Binding,我有以下xaml <DataTemplate DataType="{x:Type NameSpace:Node}"> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding Item.Value}"/> <ContentControl Content="{Binding Item}"/> &

我有以下xaml

    <DataTemplate DataType="{x:Type NameSpace:Node}">
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="{Binding Item.Value}"/>
            <ContentControl Content="{Binding Item}"/>
        </StackPanel>
    </DataTemplate>

    <DataTemplate DataType="{x:Type NameSpace:Item}">
        <TextBlock Text="{Binding Value}" />
    </DataTemplate>
        <DataTemplate DataType="{x:Type DataTemplateExample:Node}">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Item.Value}"/>
                <ContentControl Content="{Binding Item}"/>
            </StackPanel>
        </DataTemplate>

        <DataTemplate DataType="{x:Type DataTemplateExample:Item}">
            <TextBlock Text="{Binding Value}" />
        </DataTemplate>
    </Window.Resources>

    <Grid>
        <StackPanel Orientation="Vertical">
            <ContentControl Content="{Binding MyNode}"/>
            <Button Command="{Binding ChangeMyNodeItem}">Change Item</Button>
        </StackPanel>
    </Grid>
</Window>
在我的真实场景中,我使用代理,我认为这就是让WPF感到困惑的原因。项目并没有实际更改-它正在重新映射

        <DataTemplate DataType="{x:Type DataTemplateExample:Node}">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Item.Value}"/>
                <ContentControl Content="{Binding Item}"/>
            </StackPanel>
        </DataTemplate>

        <DataTemplate DataType="{x:Type DataTemplateExample:Item}">
            <TextBlock Text="{Binding Value}" />
        </DataTemplate>
    </Window.Resources>

    <Grid>
        <StackPanel Orientation="Vertical">
            <ContentControl Content="{Binding MyNode}"/>
            <Button Command="{Binding ChangeMyNodeItem}">Change Item</Button>
        </StackPanel>
    </Grid>
</Window>

最终,我使用与ShadeOfGray提出的类似的解决方案解决了这个问题。但是我应该指出,除非您使用代理,否则这是不必要的。

从您发布的内容来看,我认为您在错误的类中触发了NotifyPropertyChanged。在您的场景中,类似的内容应该可以正常工作

        <DataTemplate DataType="{x:Type DataTemplateExample:Node}">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Item.Value}"/>
                <ContentControl Content="{Binding Item}"/>
            </StackPanel>
        </DataTemplate>

        <DataTemplate DataType="{x:Type DataTemplateExample:Item}">
            <TextBlock Text="{Binding Value}" />
        </DataTemplate>
    </Window.Resources>

    <Grid>
        <StackPanel Orientation="Vertical">
            <ContentControl Content="{Binding MyNode}"/>
            <Button Command="{Binding ChangeMyNodeItem}">Change Item</Button>
        </StackPanel>
    </Grid>
</Window>
根据评论更新:

        <DataTemplate DataType="{x:Type DataTemplateExample:Node}">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Item.Value}"/>
                <ContentControl Content="{Binding Item}"/>
            </StackPanel>
        </DataTemplate>

        <DataTemplate DataType="{x:Type DataTemplateExample:Item}">
            <TextBlock Text="{Binding Value}" />
        </DataTemplate>
    </Window.Resources>

    <Grid>
        <StackPanel Orientation="Vertical">
            <ContentControl Content="{Binding MyNode}"/>
            <Button Command="{Binding ChangeMyNodeItem}">Change Item</Button>
        </StackPanel>
    </Grid>
</Window>
public class Node : INotifyPropertyChanged
{
    private Item item;

    public event PropertyChangedEventHandler PropertyChanged;

    public Item Item
    {
        get
        {
            return item;
        }

        set
        {
            item = value;

            this.NotifyPropertyChanged("Item");

            if (item != null)
            {
                item.ForcePropertyChanged("Value");
            }
        }
    }

    protected void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

public class Item : INotifyPropertyChanged
{
    private string itemValue;

    public Item()
    {
        this.Value = string.Empty;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public string Value
    {
        get
        {
            return itemValue;
        }

        set
        {
            itemValue = value;

            NotifyPropertyChanged("Value");
        }
    }

    public void ForcePropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    protected void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

谢谢你的回复。您发布的代码涵盖了直接更改Value属性的场景。。。例如:
myNode.Item.Value=newValue我感兴趣的场景是通过重新分配父项间接更改值。。。例如:
myNode.Item=newitem{Value=newValue}是的,这看起来是一个潜在的解决方案-谢谢!我希望WPF能够足够聪明地为我解决这个问题,但是如果在xaml中无法做到这一点,我将使用这个解决方案。我的Item类实际上是不可变的,所以让它触发INotifyPropertyChanged事件感觉不太正确。如果节点对象的集合是ObservableCollection,则可以完全删除ForcePropertyChanged属性。我假设您已经对Item类进行了INotifyPropertyChanged,以便查看值的更改。您是否还在节点类上实现INotifyPropertyChanged?如果没有,ContentControl将保持绑定到旧项。我已在节点类中实现了INotifyPropertyChanged,以用于项属性更改时。我没有在我的Item类中实现INotifyPropertyChanged,因为该类是不可变的。项的特定实例的Value属性从不更改。节点类是可变的,我希望在重新分配node.Item时更新UI。