【问题标题】:How can I pass data from C# to xaml?如何将数据从 C# 传递到 xaml?
【发布时间】:2012-07-21 20:52:56
【问题描述】:

我有这个 xaml 代码,需要从 C# 代码动态设置点。

<Style x:Key="Mystyle" TargetType="Button">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid>

                            <Polygon  Points="0,0 0,100 50,200" Fill="{TemplateBinding Background}"
                         Stroke="{TemplateBinding BorderBrush}"/>
                        <ContentPresenter HorizontalAlignment="Center"
                                      VerticalAlignment="Center"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

【问题讨论】:

  • 嗯。使用 ViewModel 进行数据绑定? What have you tried?
  • @Moataz A.Mohammed 发生了什么?我的解决方案不起作用?
  • 是的,我不知道为什么现在我正在尝试使用 XamlReader。因为 Windows Phone 中的 NotificationObject 类不在普通的 WPF dll 中
  • NotificationObject 在 Microsoft.Practices.Prism.dll 程序集中,您可以在普通的 WPF 应用程序中使用这个类,而不仅仅是在 Windows Phone 中。另一方面,您可以实现 interfejs INotifyPropertyChanged,一切都会好起来的。我使用了这个类,因为它是更快的解决方案。

标签: wpf xaml c#-4.0


【解决方案1】:

MVVM 解决方案:

XAML 文件:

<Window x:Class="PolygonBinding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style x:Key="Mystyle" TargetType="Button">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Grid>
                            <Polygon Points="{TemplateBinding Tag}" Fill="{TemplateBinding Background}"
                                    Stroke="{TemplateBinding BorderBrush}"/>
                            <ContentPresenter HorizontalAlignment="Center"
                                      VerticalAlignment="Center"/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <Button Style="{StaticResource Mystyle}" Tag="{Binding PointsSource}" />
    </Grid>
</Window>

代码隐藏文件:

 public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            this.DataContext = new MainViewModel();
        }
    }

ViewModel 文件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Prism.ViewModel;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Media;

namespace PolygonBinding
{
    class MainViewModel : NotificationObject
    {
        public MainViewModel()
        {
            PointsSource.Add(new Point { X = 0, Y =0 });
            PointsSource.Add(new Point { X = 0, Y = 100 });
            PointsSource.Add(new Point { X = 50, Y = 200 });            
        }

        private PointCollection _pointsSource = new PointCollection();
        public PointCollection PointsSource
        {
            get { return _pointsSource; }
            set { _pointsSource = value; RaisePropertyChanged(() => PointsSource); }
        }
    }       
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-07
    • 2011-12-09
    • 2011-06-15
    相关资源
    最近更新 更多