我在WP7应用程序页面中有一个ListBox,它绑定到名为Location的自定义对象的集合(List).在该对象中是一个名为WMO的字段,当ListBox加载时我想要做的是设置任何绑定列表框项目的foregound颜色与我的默认值具有相同的值...但我似乎无法让这个工作我读过或谷歌搜索没有任何帮助.
我知道列表中的项目绑定到数据源但我想要获得该项目的物理表示并更改前景颜色....只是无法弄清楚我是如何做到的,所以如果有人可以帮助我'我很欣赏它.
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="0,0,0,0" >
<ScrollViewer Height="615" HorizontalAlignment="Left" Margin="5,5,5,5" Name="scrollViewer1" VerticalAlignment="Top">
<ListBox Name="lbxSavedLocs" Height="615" FontSize="22" HorizontalAlignment="Left" VerticalAlignment="Top" Width="470" SelectionChanged="lbxSavedLocs_SelectionChanged" Loaded="lbxSavedLocs_Loaded">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Width="380" Text="{Binding SiteName}" HorizontalAlignment="Left" />
<TextBlock Width="90" Text="{Binding WMO}" HorizontalAlignment="Center" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</ScrollViewer>
</Grid>
Run Code Online (Sandbox Code Playgroud)
private void lbxSavedLocs_Loaded(object sender, RoutedEventArgs e)
{
//Populate the listbox from our saved locations.
lbxSavedLocs.ItemsSource = gl.savedLocs.OrderBy(x => x.SiteName);
foreach (Location itm in lbxSavedLocs.Items)
{
if (loc.WMO == gl.defaultWMO)
{
//GET AN "INVALID CAST" EXCEPTION HERE: …Run Code Online (Sandbox Code Playgroud) 我正在编写一个 UWP 应用程序来跟踪观看/购买/流式传输等的电视节目,并且我非常疯狂地尝试在 DataTempate 中获取网格列以拉伸它们的宽度,因为 XAML 中似乎存在一个错误,它忽略了 * 宽度定义。我需要 ListView 中的第一列(节目标题)来占用剩余空间(因此列定义 = "*"),虽然它会在 HeaderTemplate 中这样做,但它绝对拒绝在 DataTemplate 中这样做,所以整个网格最终会变得不稳定且不对齐,因为标题列仅使用每行所需的空间。
我的 XAML 如下 - 在 ItemTemplate DataTemplate 模板中,我绑定到一个名为 TVShow 的对象实例,该实例位于我的主视图模型的可观察集合中。(我没有在此处包含 ViewModel 或 TVShow 类定义,因为我知道这纯粹是 XAML 问题)。
到目前为止唯一有效的是在我的 TVShow 类中有一个额外的属性来存储列的正确宽度(通过从网格大小中减去其他三列的宽度(在后面的视图代码中获取),但这会导致整个列表在最初显示后重新格式化,看起来很难看,更不用说糟糕的编程了。
所以我正在寻找关于如何解决这个问题的想法 - 我可以在主视图模型中移动正确列宽的属性,但是如果我绑定到“TVShow”,我该如何绑定到模板中的那个?或者我是否必须从 DataTemplate 中取出内容并放入 UserControl?我已经在如此简单的事情上浪费了太多时间——这个错误似乎自 WPF 以来就存在,为什么 MS 从来没有解决过这个问题——非常令人沮丧。
<HubSection Name="hsShows" Width="{Binding HubSectionWidth}" MinWidth="430" MaxWidth="640"
VerticalAlignment="Top" HorizontalAlignment="Stretch" Background="{StaticResource Dark}" >
<HubSection.Header>
<TextBlock Text="Shows" TextLineBounds="TrimToBaseline" OpticalMarginAlignment="TrimSideBearings"
FontSize="24" Foreground="{StaticResource Light}"/>
</HubSection.Header>
<DataTemplate x:DataType="local:MainPage">
<ListView Name="lvwShows"
Width="{Binding HubSectionGridWidth}"
Grid.Row="0"
Foreground="{StaticResource Light}"
Background="{StaticResource Dark}"
Margin="-14,20,0,0"
Loaded="lvwShows_Loaded"
ItemsSource="{Binding …Run Code Online (Sandbox Code Playgroud)