我正在尝试ListView用WPF和C#显示数据,我对我看到的不同示例和方法感到困惑.我正在寻找一个类似于我的程序的完整工作示例,或者一个使它工作的先决条件列表.如果我设法只显示我的收藏中的一行数据,我会很高兴.目前,列表视图不显示任何内容.
C#:
public partial class MainWindow : Window
{
public ObservableCollection<Row> Rows { get; set; }
public MainWindow()
{
InitializeComponent();
Rows = new ObservableCollection<Row>();
Rows.Add(new Row
{
ID = "42",
Category = "cat",
CharLimit = 32,
Text = "Bonjour"
});
}
}
public class Row
{
public string ID { get; set; }
public string Category { get; set; }
public int CharLimit { get; set; }
public string Text { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
XAML:
<ListView ItemsSource="{Binding …Run Code Online (Sandbox Code Playgroud) 我在XAML中构建了一个UI块,它代表了一个可自定义的网页测试自动化.到目前为止没问题.我现在正在尝试将这些UI元素从XAML转换为后面的C#代码,因此我可以动态生成UI块,并且存在一些我无法解释的差异:从后面的代码生成时,不会显示多个元素.
比较截图:

XAML代码:
<StackPanel x:Name="TestPanel" Orientation="Vertical" Grid.RowSpan="2">
<Grid Margin="0,20,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="100*"/>
<ColumnDefinition Width="auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<CheckBox Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" Margin="10,0,0,0">Page link here</CheckBox>
<TextBlock Grid.Column="2" Grid.Row="0" Margin="0,0,10,0">Status here</TextBlock>
<CheckBox Grid.Column="0" Grid.Row="1" Margin="30,8,0,0">Url to check:</CheckBox>
<TextBox Grid.Column="1" Grid.Row="1" Margin="5,5,5,0">Url here</TextBox>
<TextBlock Grid.Column="2" Grid.Row="1" VerticalAlignment="Center" Margin="0,0,10,0">Status here</TextBlock>
<CheckBox Grid.Column="0" Grid.Row="2" Margin="30,8,0,0">Text to check:</CheckBox>
<TextBox Grid.Column="1" Grid.Row="2" Margin="5,5,5,0">Text here</TextBox>
<TextBlock Grid.Column="2" Grid.Row="2" VerticalAlignment="Center" Margin="0,0,10,0">Status here</TextBlock>
</Grid>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)
C#代码背后:
public partial class MainWindow : Window …Run Code Online (Sandbox Code Playgroud) 我的应用程序使用 C# 和 WPF(.net framework 4.0)运行。我的目标是拥有一个 DataGrid,其中单元格中的文本被省略号修剪,并且只有当单元格中的文本被实际修剪时才会自动显示带有完整文本的工具提示。
解决方案 1:我目前正在使用它来了解文本是否被修剪:http : //tranxcoder.wordpress.com/2008/10/12/customizing-lookful-wpf-controls-take-2/ 问题是它仅在我调整列大小时才有效。首次加载 DataGrid、对列进行排序或更新 DataGrid 的 ItemSource 时,不会显示工具提示。
解决方案 2:我也试过这个:http : //www.scottlogic.com/blog/2011/01/31/automatically-showing-tooltips-on-a-trimmed-textblock-silverlight-wpf.html 但是工具提示永远不会出现在我的 DataGrid 单元格上,但它可以很好地处理孤立的文本块。
我正在寻找简单的方法来改进解决方案 1 并使其在所有情况下都能在我的 DataGrid 中工作,或者可能采用不同的方法。
解决方案 1 的样式:
<UserControl.Resources>
<Style x:Key="TextColumnElementStyle" TargetType="TextBlock" BasedOn="{StaticResource TextBlockService}">
<Style.Setters>
<Setter Property="TextWrapping" Value="NoWrap" />
<Setter Property="TextTrimming" Value="WordEllipsis" />
</Style.Setters>
</Style>
</UserControl.Resources>
Run Code Online (Sandbox Code Playgroud)
解决方案 1 的 DataGrid:
<DataGrid ItemsSource="{Binding IssueList}" tbs:TextBlockService.AutomaticToolTipEnabled="True">
<DataGrid.Columns>
<DataGridTextColumn Header="Description" Binding="{Binding Description}"
ElementStyle="{StaticResource TextColumnElementStyle}">
</DataGrid.Columns>
</DataGrid>
Run Code Online (Sandbox Code Playgroud)
谢谢