Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.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_Xaml_Mvvm_Combobox - Fatal编程技术网

C# WPF组合框截断项

C# WPF组合框截断项,c#,wpf,xaml,mvvm,combobox,C#,Wpf,Xaml,Mvvm,Combobox,我发现将ComboBox与枚举项一起使用时出现了一种奇怪的行为。我注意到当我点击组合框时显示条目的弹出窗口会截断长项目。我发现这是因为我定义了一个固定宽度的TextBlock样式。奇怪的是,当我使用枚举项时,宽度只影响组合框。如果我改用字符串,就不会发生这种情况 这是一张发生了什么的照片。第三项应为“VeryLongTypeName” 下面是根据MVVM模式编写的代码示例 UserControl XAML: <UserControl.DataContext> <loc

我发现将ComboBox与枚举项一起使用时出现了一种奇怪的行为。我注意到当我点击组合框时显示条目的弹出窗口会截断长项目。我发现这是因为我定义了一个固定宽度的TextBlock样式。奇怪的是,当我使用枚举项时,宽度只影响组合框。如果我改用字符串,就不会发生这种情况

这是一张发生了什么的照片。第三项应为“VeryLongTypeName”

下面是根据MVVM模式编写的代码示例

UserControl XAML:

<UserControl.DataContext>
    <local:SampleViewModel/>
</UserControl.DataContext>
<StackPanel>
    <StackPanel.Resources>
        <Style TargetType="TextBlock">
            <Setter Property="Width" Value="70"/>
            <Setter Property="Margin" Value="0,0,5,0"/>
        </Style>
    </StackPanel.Resources>
    <DockPanel>
        <TextBlock Text="Items"/>
        <ComboBox ItemsSource="{Binding ItemsList}" SelectedItem="{Binding Item}"/>
    </DockPanel>
    <DockPanel>
        <TextBlock Text="String Items"/>
        <ComboBox ItemsSource="{Binding StringItemsList}" SelectedItem="{Binding StringItem}"/>
    </DockPanel>
</StackPanel>

SampleViewModel代码:

public class SampleViewModel
{
    public enum SomeType { Type1, Type2, VeryLongTypeName };

    public IEnumerable<SomeType> ItemsList
    {
        get { return (SomeType[])Enum.GetValues(typeof(SomeType)); }
    }

    public SomeType Item { get { return ItemsList.First(); } set { } }

    public IEnumerable<string> StringItemsList
    {
        get { return ItemsList.Select(type => type.ToString()); }
    }

    public string StringItem { get { return StringItemsList.First(); } set { } }
}
公共类SampleViewModel
{
公共枚举SomeType{Type1,Type2,VeryLongTypeName};
公共IEnumerable项目列表
{
获取{return(SomeType[])Enum.GetValues(typeof(SomeType));}
}
公共SomeType项{get{return ItemsList.First();}set{}
公共IEnumerable StringItemsList
{
获取{return ItemsList.Select(type=>type.ToString());}
}
公共字符串StringItem{get{return StringItemsList.First();}set{}
}
如果您构建了代码示例,在图片下面的第二个组合框中,字符串值的处理会很顺利

我有以下问题:

  • 为什么更改类型会影响图形

  • 使用enum时如何修复组合框显示


  • 欢迎任何帮助。

    您的文本块样式适用于所有文本块。组合框的内容也显示为textblocks,您将textblocks的宽度限制为70。
    为您的样式使用一个键或为组合框设置另一个文本块样式。

    使用列表框列出项目时也会出现此问题。我使用Live Property Explorer来查看发生了什么。这两种情况都在文本块中呈现内容,但仅当使用枚举值时,才会应用定义为资源的样式。我不知道为什么会这样,但事实就是这样

    为了解决enum和可能除string以外的其他类型的问题,我根据@Mardukar的想法添加了以下样式:

    <Style TargetType="ComboBoxItem">
        <Style.Resources>
            <Style TargetType="TextBlock" BasedOn="{x:Null}"/>
        </Style.Resources>
    </Style>
    
    
    
    @Fredrik关于更改ComboBox.ItemTemplate的想法也适用


    对于ListBox,样式需要具有TargetType ListBoxItem或ListBox。

    组合框中的项目是否也可以被视为类型
    TextBlock
    ?因为看起来是一样的宽度。我对WPF了解不多,但也许你不应该以这种全局方式应用你的风格。如果你设置ComboBox.ItemTemplate会怎么样?@nyrguds是的,我也注意到了。我在帖子中提到,组合框中的项目由于文本框样式而被截断。如果删除该样式,则项目显示良好。@Fredrik Ok,可以。我使用了一个带有文本框的DataTemplate,该文本框的文本为=“{Binding}”。谢谢第一个问题呢?
    StringItem=StringItemsList[0]是正确的方法。我考虑过了。我希望避免为每个文本框指定样式。考虑到在我的主要应用程序中,我定义了DokPoad的样式,并在其中包含了TrutBox样式。不管怎样,为什么第二个ComboBox不出现这种情况?ComboboxItem的样式是什么?你给了我一个好主意。我为ComboBox创建了一个样式,并在其中添加了一个TextBlock样式BasedOn=“{x:Null}”。这解决了第二个问题中提出的问题。谢谢