无法用XAML中固体刷的颜色属性结合属性



嗨,我正在尝试将solidcolorbrush的颜色属性绑定在运行时更改颜色,但即使在属性值中也没有任何颜色。

这里的菜单:

   public static string mycolor { get; set; } = "Red"

下面是我的xaml:

<my:FlipViewItemControl  x:Name="myflipView" FlipView="{Binding ElementName=flipView}"
                ItemTemplate="{StaticResource CustomItemTemplate}"
                Margin="0">
                        <ListBox.ItemsPanel>
                            <ItemsPanelTemplate >
                                <StackPanel x:Name="ScrollListBox" HorizontalAlignment="Stretch"   Orientation="Horizontal">
                                    <StackPanel.Background>
                                        <SolidColorBrush  Color="{Binding mycolor, Mode=OneWay}" Opacity="0.9"></SolidColorBrush>
                                    </StackPanel.Background>
                                </StackPanel>
                            </ItemsPanelTemplate>
                        </ListBox.ItemsPanel>
                    </my:FlipViewItemControl>

请帮助我解决这个问题。预先感谢。

SolidColorbrush的颜色属性是颜色类型,因此您应该使用

public static Color mycolor { get; set; } = Colors.Red

在XAML中,您只需将"红色"作为文本写,它在幕后为您处理。

另一个选项是将ivalueconverter添加到您的绑定中,如果由于某种原因您需要它是一个普通的字符串。

无法用XAML中的固体刷子的颜色属性绑定属性

如果您只想更改ItemsPanel颜色,请在XAML中设置StackPanel的修复值。您不需要绑定即可执行此操作,因为上述控件中只有一个ItemsPanel。如果您确实需要使用绑定来实现此功能,则绑定代码应如下:

<ItemsPanelTemplate>
    <StackPanel
        x:Name="ScrollListBox"
        HorizontalAlignment="Stretch"
        Orientation="Horizontal">
        <StackPanel.Background>
            <SolidColorBrush Opacity="0.9" Color="{Binding}" />
        </StackPanel.Background>
    </StackPanel>
</ItemsPanelTemplate>

背后的代码
 public static string mycolor { get; set; } = "Red";  
 public MainPage()
 {
     this.InitializeComponent();
     this.DataContext = mycolor;
     ...
 }

更多详细信息,请参考深度绑定数据。

ItemsPanelItemsPanelTemplate定义了用于布局的面板,而不是每个项目。如果您希望列表中的每个项目都与颜色属性绑定,则应在ItemTemplate中设置绑定。例如:

<ListBox x:Name="myflipView" Margin="0">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel
                x:Name="ScrollListBox"
                HorizontalAlignment="Stretch"
                Orientation="Horizontal">
                <StackPanel.Background>
                    <SolidColorBrush Opacity="0.9" Color="{Binding mycolor, Mode=OneWay}" />
                </StackPanel.Background>
                <TextBlock Text="{Binding Id}" />
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel
                x:Name="ScrollListBox"
                HorizontalAlignment="Stretch"
                Orientation="Horizontal">
                <StackPanel.Background>
                    <SolidColorBrush Opacity="0.9" Color="Azure" />
                </StackPanel.Background>
            </StackPanel>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
</ListBox>

最新更新