WPF Привязка простого вопроса с классом в Codebehind - PullRequest
0 голосов
/ 18 мая 2011

У меня есть элемент управления WPF с классом в codebehind.

Public Class SimpleDrawingPlugin
    Implements PluginInterface.IPluginControl
    Private _PluginInfo As New PluginInterface.clsPluginBase


    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        _PluginInfo.Name = "Simple drawing "
        _PluginInfo.Description = "Drawing of circles and rectangles"
        _PluginInfo.Icon = _PluginInfo.BitmapToBitmapImage(My.Resources.SimpleDrawing)
        _PluginInfo.Vendor = "Timo Böhme, 2011"
        _PluginInfo.FillColor = Colors.Orange '<-- Property to set to control

        Me.Ellipse1.DataContext = Me.PluginInfo '<-- Binding this Class
    End Sub


    Public ReadOnly Property PluginInfo As PluginInterface.IAdvancedControl Implements PluginInterface.IPluginControl.PluginInfo
        Get
            Return Me._PluginInfo
        End Get
    End Property
End Class

И XAML:

<UserControl x:Class="SimpleDrawingPlugin"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <Ellipse x:Name="Ellipse1" >
            <Ellipse.Stroke>
                <SolidColorBrush  Color="Red"/>
            </Ellipse.Stroke>
            <Ellipse.Fill>
                <SolidColorBrush Color="{Binding Path=FillColor}"/> <!-- Does not work -->
            </Ellipse.Fill>
        </Ellipse>
    </Grid>
</UserControl>

Привязка данных с "Path = FillColor" не работает и не обновляетсялюбое значение цвета в элементе управления.Какой синтаксис рекомендуется для привязки цвета к любому собственному свойству класса в Codebehind?

Изменить Если я использую следующий код, цвет остается оранжевым и не изменится на желтый.

Private Sub SimpleDrawingPlugin_Loaded(sender As Object, e As System.Windows.RoutedEventArgs) Handles Me.Loaded
    _PluginInfo.FillColor = Colors.Yellow
End Sub

Ответы [ 2 ]

3 голосов
/ 18 мая 2011

Я бы заменил FillColor as Color на FillBrush as SolidColorBrush. Затем сделайте это:

_PluginInfo.FillBrush = new SolidColorBrush(Colors.Orange)

Тогда в вашем xaml:

<Ellipse x:Name="Ellipse1" Stroke="Red" Fill="{Binding FillBrush}" />
2 голосов
/ 18 мая 2011

SolidColorBrush не имеет свойства DataContext, поэтому он не собирается наследовать DataContext Ellipse. Вам нужно сделать что-то вроде:

<SolidColorBrush Color="{Binding Path=DataContext.FillColor, ElementName=Ellipse1}"/>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...