0

telrikグリッドビューで円を表示しようとしていて、色をこの円に動的にバインドしたかったのですが、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) };
}
4

2 に答える 2

0

バインディングRelativeSourceの代わりに使用してみてくださいElementName

過去に同様の問題が発生し、ElementNameを使用してバインディングを解決できなかったが、RelativeSourceを使用して解決できました。バインディングを解決するタイミングに関係しているのではないかと思います。

<Ellipse Fill="{Binding RelativeSource={RelativeSource 
             AncestorType={x:Type myColour:MainPage}}, 
             Path=DataContext.Colour}" />
于 2011-07-18T15:09:42.280 に答える
0

レイアウトグリッドの子として新しい楕円をレイアウトに追加し、ビューモデルの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を指定したときにバインドが機能し始めた理由を誰かに教えてもらえますか?

誰かがこの質問に答えるまで、私はこれを答えとしてマークしています。

于 2011-08-04T15:48:46.843 に答える