将DataContext从一个用户控件复制到另一个用户控件

时间:2018-03-17 15:22:05

标签: c# wpf

我有几个Usercontrol实例,名为" ChannelControls"其中包含相当多的不同基本控件:滑块,组合框,按钮等。

但它也包含Usercontrol我做的:

<CMiX:CMiXListBox  x:Name="FileNameList"
        SelectionChanged="FileNameList_SelectionChanged"
        VerticalContentAlignment="Center"
        HorizontalContentAlignment="Right"
        ScrollViewer.HorizontalScrollBarVisibility="Disabled"
        AllowDrop="True"
        PreviewDrop="FileListBox_Drop"
        PreviewMouseDown="FileNameList_PreviewMouseDown"
        PreviewMouseMove="FileListBox_PreviewMouseMove"
        Template="{DynamicResource BaseListBoxControlStyle}"
        ItemsSource="{Binding SelectedItems, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type CMiX:FileSelector}}}"
        SelectionMode="{Binding ModeSelection,Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type CMiX:FileSelector}}}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel  Orientation="Horizontal">
                <Button x:Name="DeleteFileName" Content="X" Click="DeleteFileName_Click" Style="{StaticResource BaseButtonStyle}" 
                        Padding="4" BorderBrush="Transparent"
                        Background="Transparent" 
                        Foreground="{StaticResource BaseText}" 
                        Width="{Binding ActualHeight, ElementName=DeleteFileName}"/>
                <TextBlock Padding="4" Text="{Binding FileName, Mode=TwoWay, Converter={StaticResource PathToFileName}, UpdateSourceTrigger=PropertyChanged}"/>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem" BasedOn="{StaticResource BaseListBoxItemStyle}">
            <Setter Property="IsSelected" Value="{Binding FileIsSelected, Mode=TwoWay}"/>
            <Setter Property="VerticalContentAlignment" Value="Bottom"/>
        </Style>
    </ListBox.ItemContainerStyle>
</CMiX:CMiXListBox>

现在,当我将DataContext从一个ChannelControls实例复制并粘贴到另一个实例时,每个控件都会正确更新,但是,这个&#34; CMiXListBox&#34;现在链接到数据来自的那个。这意味着任何其他已收到新DataContext的更改都会发生。

这就是复制和粘贴的方式:

ChannelData copy = new ChannelData();

private void LayerButton_KeyDown(object sender, KeyEventArgs e)
{

    if (e.Key == Key.C && ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control))
    {
        foreach(ChannelControls cc in Utils.FindVisualChildren<ChannelControls>(cmix))
        {
            if(cc.IsVisible == true)
            {
                copy = (ChannelData)cc.DataContext;
            }
        }
    }

    if (e.Key == Key.V && ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control))
    {
        foreach (ChannelControls cc in Utils.FindVisualChildren<ChannelControls>(cmix))
        {
            if (cc.IsVisible == true && copy != null)
            {
                cc.EnabledOSC = false;

                cc.DataContext = (ChannelData)copy.Clone();

                message.SendAll(cc.DataContext, cc.Name);
                cc.EnabledOSC = true;
            }
        }
    }

我认为ItemsSources有什么问题,有什么想法吗? THX

0 个答案:

没有答案