6

我是 XAML 和数据绑定的新手。我想定义一个 GUI 控件MainWindow.xaml,它从MainWindow.xaml.cs. 为简单起见,我只是制作了一个显示计数器和递增计数器的按钮的程序。

根据我查找的早期线程,我想出了以下代码:

主窗口.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace XAMLBindingTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private int Counter
        {
            get { return (int)GetValue(CounterProperty); }
            set { SetValue(CounterProperty, value); }
        }
        public static readonly DependencyProperty CounterProperty = 
            DependencyProperty.Register("Counter", typeof(int), typeof(MainWindow), new PropertyMetadata(null));

        public MainWindow()
        {
            Counter = 0;
            InitializeComponent();
        }

        private void incrementCounter(object sender, RoutedEventArgs e)
        {
            ++Counter;
        }
    }
}

主窗口.xaml

<Window x:Class="XAMLBindingTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="140" Width="180">
    <Grid>
        <StackPanel HorizontalAlignment="Left" VerticalAlignment="Top">
            <TextBlock x:Name="txbCounter" HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding Counter}" VerticalAlignment="Top"/>
            <Button x:Name="btnIncrement" Content="Increment" Width="75" Click="incrementCounter"/>
        </StackPanel>
    </Grid>
</Window>

此示例编译,但未TextBlock显示计数器值。如何以正确的方式TextBlock连接到会员?Counter

4

2 回答 2

8

尝试将“名称”添加到您的窗口并使用“元素名称”进行绑定

<Window x:Class="XAMLBindingTest.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" Name="UI">
    <Grid>
        <StackPanel HorizontalAlignment="Left" VerticalAlignment="Top">
            <TextBlock x:Name="txbCounter" HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding ElementName=UI, Path=Counter}" VerticalAlignment="Top"/>
            <Button x:Name="btnIncrement" Content="Increment" Width="75" Click="incrementCounter"/>
        </StackPanel>
    </Grid>

</Window>
于 2012-11-26T08:21:47.730 回答
1

如果我理解正确:

ItemSource="{Binding Path=TestData, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type common:LayoutAwarePage}}}"

TestDataObservableCollection是在 .xaml.cs 文件中声明的类型的属性。ItemSource- 是您要绑定到的示例属性。

UPD2:

<ItemsControl ItemSource="{Binding Path=TestData, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type common:LayoutAwarePage}}}">
      <ItemsControl.ItemTemplate>
          <DataTemplate>
              <TextBlock Text="{Binding}" />
          </DataTemplate>
      </ItemsControl.ItemTemplate>
</ItemsControl>
于 2012-11-20T12:36:24.777 回答