简体   繁体   English

无法在XAML中将属性与SolidColorBrush的颜色属性绑定

[英]Unable to bind property with color attribute of SolidColorBrush in xaml

Hi I am trying to bind property in color attribute of SolidColorBrush to change the color at runtime but I am not getting any color even in the property value is coming. 嗨,我正在尝试在SolidColorBrush的color属性中绑定属性以在运行时更改颜色,但是即使该属性值即将到来,我也无法获得任何颜色。

Here the porperty: 这里的宝物:

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

Below is my XAML: 以下是我的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>

Please help me with the issue. 请帮我解决这个问题。 thanks in advance. 提前致谢。

The Color property of SolidCOlorBrush is of Color type, so you should use SolidCOlorBrush的Color属性是Color类型,因此您应该使用

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

In XAML you can just write "red" as text and it is handled for you behind the scenes. 在XAML中,您可以仅将“红色”写为文本,并在幕后为您处理。

Another option would be to add IValueCOnverter to your binding if, for some reason, you need it to be a plain string. 如果出于某种原因您需要将IValueCOnverter用作纯字符串,则另一个选择是将IValueCOnverter添加到您的绑定中。

Unable to bind property with color attribute of SolidColorBrush in xaml 无法在XAML中将属性与SolidColorBrush的颜色属性绑定

If you just want to change the ItemsPanel color, set a fix value for the StackPanel in XAML. 如果只想更改ItemsPanel颜色,请在XAML中为StackPanel设置一个修复值。 You don't need binding to do this since there is only one ItemsPanel in above control. 您不需要绑定即可执行此操作,因为上述控件中只有一个ItemsPanel If you do need to use binding to implement this, binding code should as follows: 如果确实需要使用绑定来实现此目的,则绑定代码应如下所示:

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

Code behind 后面的代码

 public static string mycolor { get; set; } = "Red";  
 public MainPage()
 {
     this.InitializeComponent();
     this.DataContext = mycolor;
     ...
 }

More details please reference Data binding in depth . 更多详细信息,请参阅数据绑定的详细信息。

The ItemsPanelTemplate of ItemsPanel defines the panel to use for the layout of the items, not for every item. ItemsPanelTemplateItemsPanel定义用于项目布局(而不是每个项目)的面板。 If you want every item in the list binding with the color property, you should set the binding inside ItemTemplate . 如果希望列表中的每个项目都具有color属性绑定,则应在ItemTemplate设置绑定。 For example: 例如:

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM