Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 按钮命令未触发WPF_C#_Wpf - Fatal编程技术网

C# 按钮命令未触发WPF

C# 按钮命令未触发WPF,c#,wpf,C#,Wpf,虽然这应该行得通,但事实并非如此 我的一个用户控件中包含以下代码: <ItemsControl DockPanel.Dock="Bottom" Height="45" DataContext="{Binding BottomOptions}" ItemsSource="{Binding FirstOptions, Mode=OneWay}"> <ItemsControl.Template> <ControlTemplate>

虽然这应该行得通,但事实并非如此 我的一个用户控件中包含以下代码:

<ItemsControl DockPanel.Dock="Bottom" Height="45" DataContext="{Binding BottomOptions}" ItemsSource="{Binding FirstOptions, Mode=OneWay}">
    <ItemsControl.Template>
        <ControlTemplate>
            <UniformGrid Rows="1" IsItemsHost="True" Background="Black"/>
        </ControlTemplate>
    </ItemsControl.Template>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Button CommandParameter="{Binding OptionName, Mode=OneWay}" 
                    Command="{Binding DataContext.BottomOptionSelected, RelativeSource={RelativeSource AncestorType=UserControl}}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
                <Border HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="{Binding BackgroundColor}" BorderThickness="0,0,1,0" BorderBrush="#FF090909">
                    <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
                        <Image Source="{Binding Image, Mode=OneWay}" Width="20" Height="20" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                        <TextBlock Text="{Binding OptionName, Mode=OneWay}" Foreground="{Binding ForegroundColor}" FontFamily="{Binding DataContext.BasicFont, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type base:BaseApplication}}}" FontSize="10" HorizontalAlignment="Center"/>
                    </StackPanel>
                </Border>
            </Button>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

这适用于集合中除最后一项之外的所有项。 在最后一个例子中,我只需单击按钮的顶部就可以触发命令,如果我单击按钮的中部或底部,什么也不会发生。 这很奇怪,因为第一个按钮和最后一个按钮之间的代码没有区别

请帮忙

根据注释,我正在添加VM中的代码:

public class MainDataContext : Observables
{
    public BottomOption OptionsList { get; set; }

    public DelegateCommand BottomOptionSelected { get; set; }

    public MainDataContext()
    {
        BottomOptionSelected = new DelegateCommand(SelectBottomOption);
        OptionsList = new OptionsList();
        OptionsList.FirstOptions = new ObservableCollection<BottomOption>();
        OptionsList.FirstOptions.Add(new BottomOption() { OptionName = "First", Image = @"C:\FirstImage.png", BackgroundColor = Brushes.Gray });
        OptionsList.FirstOptions.Add(new BottomOption() { OptionName = "Second", Image = @"C:\SecondImage.png", BackgroundColor = Brushes.Gray });
        OptionsList.FirstOptions.Add(new BottomOption() { OptionName = "Third", Image = @"C:\ThirdImage.png", BackgroundColor = Brushes.Gray });
        OptionsList.FirstOptions.Add(new BottomOption() { OptionName = "Forth", Image = @"C:\ForthImage.png", BackgroundColor = Brushes.Gray });
        OptionsList.FirstOptions.Add(new BottomOption() { OptionName = "Fifth", Image = @"C:\FifthImage.png", BackgroundColor = Brushes.Gray });
    }

    protected virtual void SelectBottomOption(object aObject)
    {
        // Put a break point here and check if it gets here when clicking the bottom of the fifth option 
    }
}

public class BottomOption : Observables
{
    public ObservableCollection<BottomOption> FirstOptions { get; set; }
}

public class BottomOption : Observables
{
    public string OptionName { get; set; }
    public string Image { get; set; }
    public SolidColorBrush BackgroundColor { get; set; }
}
public类MainDataContext:可观察
{
公共选项列表{get;set;}
public DelegateCommand BottomOptionSelected{get;set;}
公共MainDataContext()
{
BottomOptionSelected=新建委派命令(选择BottomOption);
OptionsList=新的OptionsList();
OptionsList.FirstOptions=新的ObservableCollection();
OptionsList.FirstOptions.Add(new BottomOption(){OptionName=“First”,Image=@“C:\FirstImage.png”,BackgroundColor=brusks.Gray});
OptionsList.FirstOptions.Add(new BottomOption(){OptionName=“Second”,Image=@“C:\SecondImage.png”,BackgroundColor=brusks.Gray});
OptionsList.FirstOptions.Add(new BottomOption(){OptionName=“Third”,Image=@“C:\ThirdImage.png”,BackgroundColor=brusks.Gray});
OptionsList.FirstOptions.Add(newbottomoption(){OptionName=“Forth”,Image=@“C:\ForthImage.png”,BackgroundColor=brusks.Gray});
OptionsList.FirstOptions.Add(newbottomoption(){OptionName=“Fifth”,Image=@“C:\FifthImage.png”,BackgroundColor=brusks.Gray});
}
受保护的虚拟对象选项(对象aObject)
{
//在此处放置断点,并在单击第五个选项的底部时检查它是否到达此处
}
}
公共类选项:可观察
{
公共ObservableCollection FirstOptions{get;set;}
}
公共类选项:可观察
{
公共字符串选项名称{get;set;}
公共字符串图像{get;set;}
公共SolidColorBrush背景色{get;set;}
}

您是否超出任何边界?因此,所有按钮都适合,例如在网格中,但只有最后一个按钮将被“切断”?由于内容位于UniformGrid中,因此它会根据容器设置大小,以便所有按钮都显示出来,而不在任何内容后面+此项控件位于DockPanel内,因此任何内容都不能重叠。您能否提供一个
FirstOptions
列表的示例?你能试着检查哪个控件点击按钮吗?FirstOptions是一个类的可观察集合,它包含几个属性,你看到的是:Image-一个指向png文件的字符串OptionName-一个简单的字符串BackgroundColor-一个笔刷(在这种情况下,所选的选项是不同的颜色)(是的,我知道可以选择使用ListBox,但这不是这里的问题)我不是建议使用ListBox。你能提供一个ObservableCollection的代码示例吗?如果有人要重现这个问题,如果他们不必编写一堆代码就可以让你的当前代码正常工作,这将非常有帮助。你试过Snoop吗?