2011-02-14 75 views
0

我目前使用ICollectionView,myCollectionView綁定到ObservableCollection。該集合的內容是從ComboBox中選擇的。每個集合項myCollectionItem都有一個VisualBrush myVisualBrush作爲子項,CurrentItem的畫筆顯示在預覽面板中。WPF綁定到當前項目不更新的孩子

集合項還是一個子對象myItemChild,它包含許多用於生成滑塊的屬性。此滑塊會更改預覽面板上的屬性。

這一切都按預期工作。

當集合視圖的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> 
+0

騎士你爲什麼要在綁定中使用斜線?刪除它,寫內容=「{Binding myItemChild}」 – vorrtex 2011-02-14 16:16:01

+0

@vorrtex當我簡化代碼時,這是一個錯字。應該在myCollectionView上指示CurrentItem。我編輯了這個問題。謝謝 – rjw 2011-02-14 16:21:53

回答

0

我試圖重現你的問題,但它的工作原理沒有問題。 這裏是我的代碼隱藏:

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>