即使我将其设置为1,SelectedIndex也会变为-1

时间:2018-03-03 18:07:31

标签: c# wpf

我正在开展一个项目并且我被困住了,希望你们都能帮助我:)。

我有一个DropDownButton,问题是当它点击它时它被绑定的ObservableCollection会发生变化。我知道当你添加/删除一个ObservableCollection时,SelectedIndex被设置为-1并且SelectedItem被设置为null。知道我的解决方案是在删除ObservableCollection上的最后一个(索引1)项目并在该索引处添加一个新项目后立即将SelectedIndex设置为1。

但是当我点击按钮时,ObservableCollection就像我说的那样被修改,SelectedIndex被设置为1,但是在程序中DropDownButton什么都没有显示(它显示索引-1)。

我的MainWindow.xaml的代码:

<Controls:SplitButton x:Name="clipboard" 
    HorizontalContentAlignment="Center"
    HorizontalAlignment="Left"
    VerticalContentAlignment="Center"
    Width="168"
    SelectedIndex="{Binding SelectedIndex}"
    ItemsSource="{Binding CopyType}"
    VerticalAlignment="Top" Margin="302,248,0,0" Height="36" Click="clipboard_Click"
    Visibility="Hidden">
    <Controls:SplitButton.IconTemplate>
        <DataTemplate>
            <Rectangle Width="15"
           Height="20"
           Fill="{Binding RelativeSource={RelativeSource AncestorType=Button}, Path=Foreground}">
                <Rectangle.OpacityMask>
                    <VisualBrush Stretch="Fill" Visual="{StaticResource appbar_clipboard_variant}" />
                </Rectangle.OpacityMask>
            </Rectangle>
        </DataTemplate>
    </Controls:SplitButton.IconTemplate>
</Controls:SplitButton>

我的ClipboardAction类的代码(其中SelectedIndex和Observable集合被绑定)

public class ClipboardAction
{
    private ObservableCollection<string> copyType = new ObservableCollection<string> { "copy to clipboard", "copy password #1" };

    private int copyNumber = 0;

    public List<string> GeneratedPasswords { get; set; }

    public int SelectedIndex { get; set; }
    public ObservableCollection<string> CopyType { get => copyType; set => copyType = value; }
    public int CopyNumber { get => copyNumber; set => copyNumber = value; }

    public ClipboardAction(List<string> generatedPasswords)
    {
        this.GeneratedPasswords = generatedPasswords;
    }

    public string Copy()
    {
        switch (SelectedIndex)
        {
            case 0:
                Clipboard.SetText(string.Join("\r\n ", GeneratedPasswords));
                return "Copied to clipboard! :)";
            case 1:
                Clipboard.SetText(GeneratedPasswords[CopyNumber]);
                if (CopyNumber + 1 == GeneratedPasswords.Count)
                {
                    CopyNumber = 0;
                    CopyType.RemoveAt(1);
                    CopyType.Add($"copy password #{CopyNumber + 1}");
                    return "All passwords copied! :D";
                }
                else
                {
                    CopyType.RemoveAt(1);
                    CopyType.Add($"copy password #{CopyNumber + 1}");
                    SelectedIndex = 1;
                    CopyNumber++;
                    return $"Password #{CopyNumber}";
                }

            default:
                break;
        }

        return "error";
    }
}

clipboard_Click代码:

private void clipboard_Click(object sender, RoutedEventArgs e)
{
    clip_text.Content = clipboardAction.Copy();
}

0 个答案:

没有答案