Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.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# 如何将对象强制转换为当前数据透视项标题_C#_Xaml_Mvvm_Binding_Pivotitem - Fatal编程技术网

C# 如何将对象强制转换为当前数据透视项标题

C# 如何将对象强制转换为当前数据透视项标题,c#,xaml,mvvm,binding,pivotitem,C#,Xaml,Mvvm,Binding,Pivotitem,我将数据透视项与MVVM绑定,然后尝试更改标题的前景色。问题是,当我仅以编程方式添加项时,代码工作正常。现在,使用此绑定,我的头是自定义对象的头,我无法正确地强制转换它: private void Pivot_SelectionChanged(object sender, Windows.UI.Xaml.Controls.SelectionChangedEventArgs e) { if (0 < e.AddedItems.Count) {

我将数据透视项与MVVM绑定,然后尝试更改标题的前景色。问题是,当我仅以编程方式添加项时,代码工作正常。现在,使用此绑定,我的头是自定义对象的头,我无法正确地强制转换它:

private void Pivot_SelectionChanged(object sender, Windows.UI.Xaml.Controls.SelectionChangedEventArgs e)
    {
        if (0 < e.AddedItems.Count)
        {
            PivotItem currentItem = e.AddedItems[0] as PivotItem;

            if (null != currentItem)
            {
                if (null != currentItem.Header)
                {
                    (currentItem.Header as TextBlock).Foreground = new SolidColorBrush(Color.FromArgb(255, 255, 255, 255));
                }
            }
        }

        if (0 < e.RemovedItems.Count)
        {
            PivotItem currentItem = e.RemovedItems[0] as PivotItem;

            if (null != currentItem)
            {
                if (null != currentItem.Header)
                {
                    (currentItem.Header as TextBlock).Foreground = new SolidColorBrush(Color.FromArgb(64, 255, 255, 255));
                }
            }
        }
    }
这是我的XAML:

<Pivot x:Name="MyPivot" Title="MasterViewModel" Foreground="#FF888888" Style="{StaticResource PivotStyle1}" SelectionChanged="Pivot_SelectionChanged" Margin="0" Grid.Row="1" ItemsSource="{Binding MyCustomViewModel.Se}" ItemTemplate="{StaticResource MyTemplate1}">
        <Pivot.HeaderTemplate>
            <DataTemplate>
                <Grid>
                    <TextBlock Text="{Binding myProp}" Margin="0, 16, 0, 0" Foreground="#FF888888" FontSize="32" FontFamily="Segoe WP" FontWeight="Light"/>
                </Grid>
            </DataTemplate>
        </Pivot.HeaderTemplate>
    </Pivot>


您已在此处使用数据绑定。因此,与其在代码中更改相应TextBlock的前景值,不如使用绑定来更改它。在VM中添加一个属性,并将其绑定到前台和Pivot_SelectionChanged属性的更改值中。我不知道如何执行此操作,因此我宁愿坚持使用代码隐藏逻辑的先前解决方案。如何添加这样的属性?添加类似于“myProp”的属性,可以将其命名为“myForeground”,并在Pivot_SelectionChanged.中进行更改。。如果您想在代码隐藏中执行此操作,那么调试时看到的currentItem.Header的对象类型是什么?问题是myProp是在模型中定义的,而不是ViewModel(它是自动生成的)。如何在ViewModel中执行此操作?我在页面的代码隐藏中创建了一个名为HeaderForeground的属性,但它不起作用。currentItem.Header的类型是什么,您能在代码隐藏中调试并找到它吗?如果它是DependencyObject,则可以使用VisualTreeHelper获取标题的Textblock类型的子项。
<Pivot x:Name="MyPivot" Title="MasterViewModel" Foreground="#FF888888" Style="{StaticResource PivotStyle1}" SelectionChanged="Pivot_SelectionChanged" Margin="0" Grid.Row="1" ItemsSource="{Binding MyCustomViewModel.Se}" ItemTemplate="{StaticResource MyTemplate1}">
        <Pivot.HeaderTemplate>
            <DataTemplate>
                <Grid>
                    <TextBlock Text="{Binding myProp}" Margin="0, 16, 0, 0" Foreground="#FF888888" FontSize="32" FontFamily="Segoe WP" FontWeight="Light"/>
                </Grid>
            </DataTemplate>
        </Pivot.HeaderTemplate>
    </Pivot>