在 GridViewDataColumn.Header 中绑定 UIElement 的值不起作用

发布于 2024-11-24 03:52:47 字数 1889 浏览 1 评论 0原文

我试图在 telrik gridview 中显示一个圆圈,并且我想将颜色动态绑定到该圆圈。由于我正在使用 MVVM 模式,因此我必须将视图模型绑定到页面的数据上下文。但绑定似乎并不存在当我调查这个问题时,因为列标题没有任何数据上下文,所以我尝试使用“ElementName”绑定值并尝试使用其数据上下文,但即使这样也不起作用。

谁能帮我解决这个问题。

这是我的 xaml 代码

<UserControl x:Class="TelrikStyling.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:telerikGrid="http://schemas.telerik.com/2008/xaml/presentation"
             xmlns:myColour="clr-namespace:TelrikStyling"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400" x:Name="myMainPage">
        <Grid x:Name="LayoutRoot" Background="White" Loaded="LayoutRoot_Loaded">
            <telerikGrid:RadGridView x:Name="radGridView"
                        AutoGenerateColumns="False">
            <telerikGrid:RadGridView.Columns>
                <telerikGrid:GridViewDataColumn>
                    <telerikGrid:GridViewDataColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Ellipse x:Name="headerEllipse" Width="20" Height="20" Fill="{Binding ElementName=myMainPage, Path=DataContext.Colour}"></Ellipse>
                            </StackPanel>
                    </telerikGrid:GridViewDataColumn.Header>
                </telerikGrid:GridViewDataColumn>
            </telerikGrid:RadGridView.Columns>
        </telerikGrid:RadGridView>
    </Grid>
</UserControl>

这是我的视图模型

public MainPage()
{
    InitializeComponent();

    this.DataContext = new MainPageViewModel { Colour = new SolidColorBrush(Colors.Red) };
}

I am trying to show a circle in a telrik gridview and I wanted to bind the colour dynamically to this circle.Since I am working with MVVM pattern I have to bound my view model to the datacontext of my page.But the binding does not seems to be working for me.When I investigated the issue was because the column headers does not have any datacontext, so I tried bound the value using 'ElementName' and tried to use its datacontext but even that is also not working.

Could anyone please help me resolving this issue.

This is my xaml code

<UserControl x:Class="TelrikStyling.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:telerikGrid="http://schemas.telerik.com/2008/xaml/presentation"
             xmlns:myColour="clr-namespace:TelrikStyling"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400" x:Name="myMainPage">
        <Grid x:Name="LayoutRoot" Background="White" Loaded="LayoutRoot_Loaded">
            <telerikGrid:RadGridView x:Name="radGridView"
                        AutoGenerateColumns="False">
            <telerikGrid:RadGridView.Columns>
                <telerikGrid:GridViewDataColumn>
                    <telerikGrid:GridViewDataColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Ellipse x:Name="headerEllipse" Width="20" Height="20" Fill="{Binding ElementName=myMainPage, Path=DataContext.Colour}"></Ellipse>
                            </StackPanel>
                    </telerikGrid:GridViewDataColumn.Header>
                </telerikGrid:GridViewDataColumn>
            </telerikGrid:RadGridView.Columns>
        </telerikGrid:RadGridView>
    </Grid>
</UserControl>

This is my view model

public MainPage()
{
    InitializeComponent();

    this.DataContext = new MainPageViewModel { Colour = new SolidColorBrush(Colors.Red) };
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清旖 2024-12-01 03:52:51

我找到了解决我的问题的方法,即通过将新椭圆作为布局网格的子项添加到布局中,然后将视图模型的 Color 属性绑定到新椭圆的 Fill 属性。后来我绑定了椭圆的 Fill 属性在 RadGrid 中添加到新添加的椭圆。

这是新的 xaml

<UserControl x:Class="TelrikStyling.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:telerikGrid="http://schemas.telerik.com/2008/xaml/presentation"
             xmlns:myColour="clr-namespace:TelrikStyling"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400" x:Name="myMainPage">
        <Grid x:Name="LayoutRoot" Background="White" Loaded="LayoutRoot_Loaded">
            <telerikGrid:RadGridView x:Name="radGridView"
                        AutoGenerateColumns="False">
            <telerikGrid:RadGridView.Columns>
                <telerikGrid:GridViewDataColumn>
                    <telerikGrid:GridViewDataColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Ellipse x:Name="headerEllipse" Width="20" Height="20" Fill="{Binding ElementName=invisibleEllipse, Path=Fill}"></Ellipse>
                            </StackPanel>
                    </telerikGrid:GridViewDataColumn.Header>
                </telerikGrid:GridViewDataColumn>
            </telerikGrid:RadGridView.Columns>
        </telerikGrid:RadGridView>
   <Ellipse x:Name="invisibleEllipse" Width="20" Height="20" Fill="{Binding Colour}"></Ellipse>
    </Grid>
</UserControl>

谁能告诉我为什么当我给出 myMainPage 作为元素名称时绑定不起作用,而当我给出新的 elisple 时它开始工作?

在有人回答这个问题之前,我将其标记为答案。

I have found a solution for my problem that is by adding new ellipse to the layout as a child of the layout grid and then bind the Colour property of the viewmodel to the Fill property of new ellipse.Later I have bound Fill property of the ellipse in the RadGrid to the newly added ellipse.

This is the new xaml

<UserControl x:Class="TelrikStyling.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:telerikGrid="http://schemas.telerik.com/2008/xaml/presentation"
             xmlns:myColour="clr-namespace:TelrikStyling"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400" x:Name="myMainPage">
        <Grid x:Name="LayoutRoot" Background="White" Loaded="LayoutRoot_Loaded">
            <telerikGrid:RadGridView x:Name="radGridView"
                        AutoGenerateColumns="False">
            <telerikGrid:RadGridView.Columns>
                <telerikGrid:GridViewDataColumn>
                    <telerikGrid:GridViewDataColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Ellipse x:Name="headerEllipse" Width="20" Height="20" Fill="{Binding ElementName=invisibleEllipse, Path=Fill}"></Ellipse>
                            </StackPanel>
                    </telerikGrid:GridViewDataColumn.Header>
                </telerikGrid:GridViewDataColumn>
            </telerikGrid:RadGridView.Columns>
        </telerikGrid:RadGridView>
   <Ellipse x:Name="invisibleEllipse" Width="20" Height="20" Fill="{Binding Colour}"></Ellipse>
    </Grid>
</UserControl>

Can anyone tell me why the binding was not working when I gives myMainPage as the element name and it started working when I given a new elisple?

Untill someone answer to this question, I am marking this as an answer.

ゞ花落谁相伴 2024-12-01 03:52:50

尝试在绑定中使用 RelativeSource 而不是 ElementName

我过去也遇到过类似的问题,无法使用 ElementName 解析绑定,但可以使用relativeSource。我认为这可能与解决绑定的时间有关。

<Ellipse Fill="{Binding RelativeSource={RelativeSource 
             AncestorType={x:Type myColour:MainPage}}, 
             Path=DataContext.Colour}" />

Try using RelativeSource instead of ElementName in your Binding

I've had similar issues in the past where I couldn't resolve a binding using ElementName but could with RelativeSource. I think it may have to do with the timing of resolving the binding.

<Ellipse Fill="{Binding RelativeSource={RelativeSource 
             AncestorType={x:Type myColour:MainPage}}, 
             Path=DataContext.Colour}" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文