WPF 绑定到当前项目的子项未更新

发布于 2024-10-17 05:55:33 字数 2552 浏览 5 评论 0原文

我目前正在使用 ICollectionView、myCollectionView 绑定到 ObservableCollection。该集合的内容是从组合框中选择的。每个集合项 myCollectionItem 都有一个 VisualBrush myVisualBrush 作为子项,并且 CurrentItem 的画笔显示在预览面板中。

集合项还有一个子对象 myItemChild,它包含许多自己的属性,用于生成滑块。此滑块更改预览面板上的属性。

这一切都按预期进行。

当 Collectionview 的 CurrentItem 更改时,预览面板会正确更新,但滑块继续显示前一个 CurrentItem 的 myItemChild。

对 myItemChild 的更改没有被提出,我应该如何处理这种情况?

我很可能错过了一些明显的东西,所以任何指示都会受到赞赏。

问候

罗布

<Grid>
  <Grid.RowDefinitions>
    <RowDefinition Height="Auto"></RowDefinition>
    <RowDefinition Height="Auto"></RowDefinition>
    <RowDefinition Height="Auto"></RowDefinition>
  </Grid.RowDefinitions>

  <!-- Combo Box for selection of item-->
  <ComboBox Grid.Row="0" ItemsSource="{Binding myCollectionView, Mode=TwoWay}" IsSynchronizedWithCurrentItem="True">
    <ComboBox.ItemTemplate>
      <DataTemplate DataType="{x:Type vm:myCollectionItem}" >
        <StackPanel>
          <Rectangle Height="40" Width="40" Fill="{Binding myVisualBrush}"/>
        </StackPanel>
      </DataTemplate>
    </ComboBox.ItemTemplate>
  </ComboBox>

  <!-- Panel to preview item-->
  <ContentControl Grid.Row="1" Content="{Binding myCollectionView/}">
    <ContentControl.ContentTemplate>
      <DataTemplate DataType="{x:Type vm:myCollectionItem}" >
        <Rectangle Margin="20" Fill="{Binding myVisualBrush}" />
      </DataTemplate>
    </ContentControl.ContentTemplate>
  </ContentControl>

  <!-- Slider to edit item-->
  <ContentControl Grid.Row="2" Content="{Binding myCollectionView/}">
    <ContentControl.ContentTemplate>

      <DataTemplate DataType="{x:Type vm:myCollectionItem}" >
        <ContentControl Content="{Binding myItemChild}">
          <ContentControl.ContentTemplate>

            <DataTemplate DataType="{x:Type vm:myCollectionItemChild}" >
              <StackPanel>
                <Label Content="{Binding myValueLabel, Mode=OneWay}"/>
                <Slider Value="{Binding myValue, Mode=TwoWay}" Maximum="{Binding myValueMax}" Minimum="{Binding myValueMin}"/>
              </StackPanel>
            </DataTemplate>

          </ContentControl.ContentTemplate>
        </ContentControl>
      </DataTemplate>

    </ContentControl.ContentTemplate>
  </ContentControl>
</Grid>

I am currently binding to an ObservableCollection using an ICollectionView, myCollectionView. The contents of that collection are being selected from a ComboBox. Each collection item, myCollectionItem, has a VisualBrush, myVisualBrush, as a child and the CurrentItem's brush is displayed in a preview panel.

The collection item also a child object, myItemChild, which contains a number of its own properties that are used to generate a slider. This slider alters properties on the preview panel.

This all works as expected.

When the CurrentItem of the Collectionview is changed the preview panel updates correctly but the slider continues to show the previous CurrentItem's myItemChild.

The change to myItemChild is not being raised, how should I handle this situation?

Its highly probable I have missed something obvious so any pointers appreciated.

Regards

Rob

<Grid>
  <Grid.RowDefinitions>
    <RowDefinition Height="Auto"></RowDefinition>
    <RowDefinition Height="Auto"></RowDefinition>
    <RowDefinition Height="Auto"></RowDefinition>
  </Grid.RowDefinitions>

  <!-- Combo Box for selection of item-->
  <ComboBox Grid.Row="0" ItemsSource="{Binding myCollectionView, Mode=TwoWay}" IsSynchronizedWithCurrentItem="True">
    <ComboBox.ItemTemplate>
      <DataTemplate DataType="{x:Type vm:myCollectionItem}" >
        <StackPanel>
          <Rectangle Height="40" Width="40" Fill="{Binding myVisualBrush}"/>
        </StackPanel>
      </DataTemplate>
    </ComboBox.ItemTemplate>
  </ComboBox>

  <!-- Panel to preview item-->
  <ContentControl Grid.Row="1" Content="{Binding myCollectionView/}">
    <ContentControl.ContentTemplate>
      <DataTemplate DataType="{x:Type vm:myCollectionItem}" >
        <Rectangle Margin="20" Fill="{Binding myVisualBrush}" />
      </DataTemplate>
    </ContentControl.ContentTemplate>
  </ContentControl>

  <!-- Slider to edit item-->
  <ContentControl Grid.Row="2" Content="{Binding myCollectionView/}">
    <ContentControl.ContentTemplate>

      <DataTemplate DataType="{x:Type vm:myCollectionItem}" >
        <ContentControl Content="{Binding myItemChild}">
          <ContentControl.ContentTemplate>

            <DataTemplate DataType="{x:Type vm:myCollectionItemChild}" >
              <StackPanel>
                <Label Content="{Binding myValueLabel, Mode=OneWay}"/>
                <Slider Value="{Binding myValue, Mode=TwoWay}" Maximum="{Binding myValueMax}" Minimum="{Binding myValueMin}"/>
              </StackPanel>
            </DataTemplate>

          </ContentControl.ContentTemplate>
        </ContentControl>
      </DataTemplate>

    </ContentControl.ContentTemplate>
  </ContentControl>
</Grid>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巨坚强 2024-10-24 05:55:33

我尝试重现您的问题,但它可以正常工作。
这是我的隐藏代码:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        var items = new ObservableCollection<myCollectionItem>{
            new myCollectionItem(Brushes.Red, new myCollectionItemChild("Red", 15, 0, 80)),
            new myCollectionItem(Brushes.Green, new myCollectionItemChild("Green", 0.7, 0, 1)),
            new myCollectionItem(Brushes.Purple, new myCollectionItemChild("Purple", 22,11,33))};
        this.DataContext = new Model { myCollectionView = items };
    }


}

public class Model
{
    public ObservableCollection<myCollectionItem> myCollectionView { get; set; }
}

public class myCollectionItem
{
    public myCollectionItem(Brush br, myCollectionItemChild child)
    {
        this.myVisualBrush = br;
        this.myItemChild = child;
    }
    public Brush myVisualBrush { get; set; }
    public myCollectionItemChild myItemChild { get; set; }
}

public class myCollectionItemChild
{
    public myCollectionItemChild(string label, double val, double min, double max)
    {
        this.myValueLabel = label;
        this.myValue = val;
        this.myValueMin = min;
        this.myValueMax = max;
    }
    public string myValueLabel { get; set; }
    public double myValue { get; set; }
    public double myValueMax { get; set; }
    public double myValueMin { get; set; }
}

另外,您不需要使用控件模板。可以写得更清楚:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"></RowDefinition>
        <RowDefinition Height="80"></RowDefinition>
        <RowDefinition Height="Auto"></RowDefinition>
    </Grid.RowDefinitions>

    <!-- Combo Box for selection of item-->
    <ComboBox Grid.Row="0" ItemsSource="{Binding myCollectionView, Mode=TwoWay}" IsSynchronizedWithCurrentItem="True">
        <ComboBox.ItemTemplate>
            <DataTemplate DataType="{x:Type vm:myCollectionItem}" >
                <StackPanel>
                    <Rectangle Height="40" Width="40" Fill="{Binding myVisualBrush}"/>
                </StackPanel>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

    <!-- Panel to preview item-->
    <Rectangle Margin="20" Fill="{Binding myCollectionView/myVisualBrush}" Grid.Row="1" />

    <!-- Slider to edit item-->
    <StackPanel Grid.Row="2" DataContext="{Binding myCollectionView/myItemChild}">
        <Label Content="{Binding myValueLabel, Mode=OneWay}"/>
        <Slider Value="{Binding myValue, Mode=TwoWay}" Maximum="{Binding myValueMax}" Minimum="{Binding myValueMin}"/>
    </StackPanel>
</Grid>

I tried to reproduce your problem, but it works without problems.
Here is my code-behind:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        var items = new ObservableCollection<myCollectionItem>{
            new myCollectionItem(Brushes.Red, new myCollectionItemChild("Red", 15, 0, 80)),
            new myCollectionItem(Brushes.Green, new myCollectionItemChild("Green", 0.7, 0, 1)),
            new myCollectionItem(Brushes.Purple, new myCollectionItemChild("Purple", 22,11,33))};
        this.DataContext = new Model { myCollectionView = items };
    }


}

public class Model
{
    public ObservableCollection<myCollectionItem> myCollectionView { get; set; }
}

public class myCollectionItem
{
    public myCollectionItem(Brush br, myCollectionItemChild child)
    {
        this.myVisualBrush = br;
        this.myItemChild = child;
    }
    public Brush myVisualBrush { get; set; }
    public myCollectionItemChild myItemChild { get; set; }
}

public class myCollectionItemChild
{
    public myCollectionItemChild(string label, double val, double min, double max)
    {
        this.myValueLabel = label;
        this.myValue = val;
        this.myValueMin = min;
        this.myValueMax = max;
    }
    public string myValueLabel { get; set; }
    public double myValue { get; set; }
    public double myValueMax { get; set; }
    public double myValueMin { get; set; }
}

Also, you don't need to use control templates. It can be written more clearly:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"></RowDefinition>
        <RowDefinition Height="80"></RowDefinition>
        <RowDefinition Height="Auto"></RowDefinition>
    </Grid.RowDefinitions>

    <!-- Combo Box for selection of item-->
    <ComboBox Grid.Row="0" ItemsSource="{Binding myCollectionView, Mode=TwoWay}" IsSynchronizedWithCurrentItem="True">
        <ComboBox.ItemTemplate>
            <DataTemplate DataType="{x:Type vm:myCollectionItem}" >
                <StackPanel>
                    <Rectangle Height="40" Width="40" Fill="{Binding myVisualBrush}"/>
                </StackPanel>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

    <!-- Panel to preview item-->
    <Rectangle Margin="20" Fill="{Binding myCollectionView/myVisualBrush}" Grid.Row="1" />

    <!-- Slider to edit item-->
    <StackPanel Grid.Row="2" DataContext="{Binding myCollectionView/myItemChild}">
        <Label Content="{Binding myValueLabel, Mode=OneWay}"/>
        <Slider Value="{Binding myValue, Mode=TwoWay}" Maximum="{Binding myValueMax}" Minimum="{Binding myValueMin}"/>
    </StackPanel>
</Grid>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文