WPF基本绑定

时间:2014-10-08 15:39:18

标签: c# wpf xaml

我知道这已被问过很多次,但无论我读了多少教程,我根本无法理解。我有一个带有三个ColumnDefinitions的网格,可以通过两个GridSplitters调整大小。我想要的是它下面的另一个网格,其中三个ColumnDefinitions在调整顶部网格时调整大小(与iTunes这样的程序中的UI非常相似)。我想要单独网格的原因是因为最终每个网格都是它自己的对象,并且需要拖放属性。如果有人想看看我在看什么,我写的是Xaml。

<Canvas Width="400" Height="15" Background="AntiqueWhite">
        <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="400" Name='Maingrid'>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="140" MinWidth="50"/>
                <ColumnDefinition Width="116" MinWidth="50"/>
                <ColumnDefinition Width="144" MinWidth="50"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="15"/>
            </Grid.RowDefinitions>
            <GridSplitter Grid.Column="0"
          HorizontalAlignment="Right"
          VerticalAlignment="Stretch"
          Background="Black" 
          ShowsPreview="True"
          Width="2"
          />
            <GridSplitter Grid.Column="1"
          HorizontalAlignment="Right"
          VerticalAlignment="Stretch"
          Background="Black" 
          ShowsPreview="True"
          Width="2"
          />
            <TextBlock Text="Song" Grid.Column="0" Width="30"/>
            <TextBlock Text="Song" Grid.Column="1" Width="30"/>
            <TextBlock Text="Song" Grid.Column="2" Width="30"/>
        </Grid>
    </Canvas>

    <Canvas Width="400" Height="15" Background="RosyBrown" Margin="58,168,59,138">
        <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="400">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="140" MinWidth="50"/>
                <ColumnDefinition Width="116" MinWidth="50"/>
                <ColumnDefinition Width="144" MinWidth="50"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="15"/>
            </Grid.RowDefinitions>
            <TextBlock Text="Song" Grid.Column="0" Width="30"/>
            <TextBlock Text="Song" Grid.Column="1" Width="30"/>
            <TextBlock Text="Song" Grid.Column="2" Width="30"/>
        </Grid>
    </Canvas>

另外作为最后一点,我已经看过无数的数据绑定教程和指南,而我17岁的大脑似乎无法绕过它。如果我能理解如何将一个文本块绑定到我的C#代码隐藏中的变量(又名:不在代码隐藏中设置textblock&text文本属性,只是改变字符串的内容),我就是这样做的。能够变得更有成效。谢谢任何可以帮助我的人,我知道这个问题已被问过一百万次了。

2 个答案:

答案 0 :(得分:1)

在c#codebehind中,创建一个属性:

private string _songTitle;
public string SongTitle { get { return _songTitle; } set { songTitle = value; } }

在你的xaml中,创建一个绑定:

<TextBlock Text="{Binding SongTitle}" />

为绑定设置DataContext(您可以将其放在Window.Loaded事件中)

this.DataContext = this;

在您的代码中设置您的媒体资源

SongTitle = "Some words and stuff";

关于它。它可能变得更复杂,但这是基础。

答案 1 :(得分:0)

这是一种无需代码的方法。只需给出第一列定义名称,然后将较低列定义widths'绑定到第一个网格中相应列定义的宽度。干净简单,没有代码可以捣乱:

<Canvas Width="400" Height="15" Background="AntiqueWhite">
    <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="400" Name='Maingrid'>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Name="Grid1Col1" Width="140" MinWidth="50"/>
            <ColumnDefinition Name="Grid1Col2" Width="116" MinWidth="50"/>
            <ColumnDefinition Name="Grid1Col3" Width="144" MinWidth="50"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="15"/>
        </Grid.RowDefinitions>
        <GridSplitter Grid.Column="0"
      HorizontalAlignment="Right"
      VerticalAlignment="Stretch"
      Background="Black" 
      ShowsPreview="True"
      Width="2"
      />
        <GridSplitter Grid.Column="1"
      HorizontalAlignment="Right"
      VerticalAlignment="Stretch"
      Background="Black" 
      ShowsPreview="True"
      Width="2"
      />
        <TextBlock Text="Song" Grid.Column="0" Width="30"/>
        <TextBlock Text="Song" Grid.Column="1" Width="30"/>
        <TextBlock Text="Song" Grid.Column="2" Width="30"/>
    </Grid>
</Canvas>

<Canvas Width="400" Height="15" Background="RosyBrown" Margin="58,168,59,138">
    <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="400">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="{Binding Width, ElementName=Grid1Col1}" MinWidth="50"/>
            <ColumnDefinition Width="{Binding Width, ElementName=Grid1Col1}" MinWidth="50"/>
            <ColumnDefinition Width="{Binding Width, ElementName=Grid1Col1}" MinWidth="50"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="15"/>
        </Grid.RowDefinitions>
        <TextBlock Text="Song" Grid.Column="0" Width="30"/>
        <TextBlock Text="Song" Grid.Column="1" Width="30"/>
        <TextBlock Text="Song" Grid.Column="2" Width="30"/>
    </Grid>
</Canvas>