我有一个int数组,正在使用它在ItemsControl中创建一堆滑块。我在滑块上使用双向绑定,但是数组从未设置好(我在设置器上放置了一个断点)。所有这些都在UserControl中。
UserControl XAML:
<ItemsControl ItemsSource="{Binding Values, Mode=TwoWay}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Width="30" MaxWidth="30">
<Slider Orientation="Vertical" Margin="5,0,0,0"
Value="{Binding Path=., Mode=TwoWay}"
Maximum="100"
Minimum="-100"
Height="100"/>
<TextBox Text="{Binding Path=., Mode=TwoWay}" Name="NumberTextBox" PreviewTextInput="NumberValidationTextBox"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
后面的UserControl代码:
public int[] Values
{
get { return (int[])GetValue(ValuesProperty); }
set { SetValue(ValuesProperty, value); }
}
public static readonly DependencyProperty ValuesProperty =
DependencyProperty.Register("Values", typeof(int[]), typeof(Equalizer), new UIPropertyMetadata(new int[] { 0,0 }));
UserControl是在MainWindow中创建的,并在其中输入了值:
<local:Equalizer Margin="50" Height="20" Width="100" VerticalAlignment="Top"
MyText="{Binding TextData, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:MainWindow}}"
MyProperty="True"
MinValue="{Binding MinValue, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:MainWindow}}"
MaxValue="{Binding MaxValue, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:MainWindow}}"
Values="{Binding Values, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:MainWindow}, Mode=TwoWay}"/>