c#用户控件Win8中的DataBinding

时间:2013-07-17 13:25:32

标签: c# xaml data-binding

我有一个ObservableCollection列表,它从数据库接收数据,然后我通过数据绑定将这些数据放入我的网格中。 所以,当我点击这个网格的项目时,我会出现一个用户控件。我想要一个我的用户控件的文本框,显示我的网格的选定项目。 我已尝试使用数据绑定,但文本框未显示所选项目..是​​否可能?

网格代码:

<FlexGrid:C1FlexGrid 

        x:Name="grid" ItemsSource="{Binding list3, Mode=TwoWay}" 
        AutoGenerateColumns="False" 
        HorizontalAlignment="Left" Height="431" Margin="10,147,0,0" VerticalAlignment="Top" Width="1152" SelectionMode="Row" KeepCurrentVisible="True" Tapped="grid_Tapped" >
        <FlexGrid:C1FlexGrid.DataContext>
            <local:Controller/>
        </FlexGrid:C1FlexGrid.DataContext>

        <FlexGrid:C1FlexGrid.Columns>
            <FlexGrid:Column Binding="{Binding describe}" Header="Describes" Width="800" />
            <FlexGrid:Column Binding="{Binding describeNote}" Header="Describes Notes" Width="300" />
        </FlexGrid:C1FlexGrid.Columns>

    </FlexGrid:C1FlexGrid>

用户控制码:

<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Binding"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:FlexGrid="using:C1.Xaml.FlexGrid"
x:Class="Binding.popNotas"
mc:Ignorable="d" Height="281.925" Width="656.03">

<Grid>
    <TextBox x:Name="txt2" Text="{Binding SelectedItem.describe, ElementName=grid, Mode=TwoWay}" Height="38" Margin="140,5,141,0" TextWrapping="Wrap" VerticalAlignment="Top">

    </TextBox>

</Grid>

cs code

public class Controller : Common.BindableBase
{
    //DAOS      
    public TesteDao dao { get; set; }


    private ObservableCollection<ClPasso> _list3 = new ObservableCollection<ClPasso>();
    public ObservableCollection<ClPasso> list3
    {
        get { return _list3; }
        set { this.SetProperty(ref this._list3, value); }
    }


    public Controller()
    {
        OnNavigatedTo();

    }
    protected async void OnNavigatedTo()
    {
        await InitializeDatabase();
        list3 = await createlist3();
    }

    private async Task InitializeDatabase()
    {
        string datbasePath = Windows.Storage.ApplicationData.Current.LocalFolder.Path + "\\bd_example.db";
        DataBase database = new DataBase (datbasePath);
        await database.initialize();
        dao = new TesteDao(database);

    }


    public async Task<ObservableCollection<ClPasso>> createlist3()
    {
        return await dao.joinListAsync(123, "924be4cc-16db-40c2-b342-d6c1fccbec86");
    }

  }

帮助!

感谢!!!

1 个答案:

答案 0 :(得分:0)

我已经解决了我的问题..

所以,我在我的用户控件后面的代码上创建了一个DependencyProperty,名为选择,我把它放在我的文本框的文本中..之后,当我将使用我的用户控件时,在我的主页面中,我通过了属于价值的..

像这样:

用户控制

 public sealed partial class UCNotes : UserControl
{


    public string selection
    {
        get { return (string)GetValue(selectionProperty); }
        set { SetValue(selectionProperty, value); }
    }

    public static readonly DependencyProperty selecionadoProperty =
        DependencyProperty.Register("selection", typeof(string), typeof(UCNotes), new PropertyMetadata(null));

用户控制XAML

<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Test"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:FlexGrid="using:C1.Xaml.FlexGrid"
x:Class="Fiscalizacao.UCNotes"
mc:Ignorable="d"
d:DesignHeight="327.068" Width="799.248">

<Grid Margin="10,0,10,10" Background="#FFB2B2B2" Height="303" VerticalAlignment="Bottom">
    <TextBox x:Name="txtSelection" Text="{Binding selection}" Height="38" Margin="153,75,153,0" TextWrapping="Wrap" VerticalAlignment="Top" BorderBrush="Black"/>

主页XAML

 <FlexGrid:C1FlexGrid 
            x:Name="questionsGrid" 
            HorizontalAlignment="Left" Height="417" Margin="31,181,0,0" VerticalAlignment="Top" Width="1306" 
            AutoGenerateColumns="False" KeepCurrentVisible="True" 
            SelectionMode="Row" ItemsSource="{Binding list, Mode=TwoWay}">
            <FlexGrid:C1FlexGrid.Columns>
                <FlexGrid:Column Binding="{Binding describes}" Header="Descrição" Width="900" />
                <FlexGrid:Column Binding="{Binding descibesNotes}" Header="Nota" Width="*" />
            </FlexGrid:C1FlexGrid.Columns>
        </FlexGrid:C1FlexGrid>

        <Popup x:Name="popNotes" IsLightDismissEnabled="True" IsOpen="False" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Row="1" Grid.Column="1" Margin="0,0,700,300" >
            <local:UCNotes selection="{Binding SelectedItem.describes, ElementName=questionsGrid" />

        </Popup>