簡體   English   中英

wpf 自動完成框綁定

[英]wpf autocompletebox binding

我創建了一些包含一些標准控件的用戶控件,例如:文本框/組合框 + 圖像 + 文本塊。 我正在嘗試對 AutoCompleteBox 做同樣的事情,但到目前為止都失敗了......項目列表顯示得很好,我可以 select na 項目,但這不會觸發對 SelectedItem 的更改。 我對 combobox 使用幾乎相同的代碼,所以不確定出了什么問題......

無論如何,我已經在 AutoCompleteBox 上使用了 ValueMemberPath / ValueMemberBinding,但不確定這是否是通往 go 的方式。

用戶控件 xaml:

<Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="3*" />
        </Grid.ColumnDefinitions>

        <Grid Grid.Column="0" Margin="0,0,2,0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>

            <Image Source="{Binding ElementName=ACProperty, Path=ImageSource}" VerticalAlignment="Center"
                   MaxHeight="30" MaxWidth="30" Margin="1" Grid.Column="0" RenderOptions.BitmapScalingMode="HighQuality"/>

            <TextBlock Text="{Binding ElementName=ACProperty, Path=Label}" VerticalAlignment="Center"
                       HorizontalAlignment="Left" Grid.Column="1" Margin="1" TextWrapping="Wrap" Width="100" />
        </Grid>

        <toolkitInput:AutoCompleteBox FilterMode="ContainsOrdinal" IsTextCompletionEnabled="True"
                                      ItemsSource="{Binding ElementName=ACProperty, Path=ItemsSource}" 
                                      SelectedItem="{Binding ElementName=ACProperty, Path=SelectedItem}"
                                      MinimumPrefixLength="2"
                                      MinimumPopulateDelay="300"
                                      VerticalAlignment="Center"
                                      HorizontalAlignment="Stretch" Grid.Column="1" Margin="1,1,2,1" />

    </Grid>

后面的代碼:

public static DependencyProperty LabelProperty = DependencyProperty.Register(
        "Label", typeof(string), typeof(AutoCompleteProperty));

    public static readonly DependencyProperty ItemsSourceProperty =
       DependencyProperty.Register("ItemsSource", typeof(object), typeof(AutoCompleteProperty));

    public static readonly DependencyProperty SelectedItemProperty =
        DependencyProperty.Register("SelectedItem", typeof(object), typeof(AutoCompleteProperty),
        new FrameworkPropertyMetadata() { BindsTwoWayByDefault = true });

    public static DependencyProperty ImageSourceProperty = DependencyProperty.Register(
        "ImageSource", typeof(string), typeof(AutoCompleteProperty));

    public object ItemsSource
    {
        get
        {
            return (object)GetValue(ItemsSourceProperty);
        }
        set
        {
            SetValue(ItemsSourceProperty, value);
        }
    }

    public object SelectedItem
    {
        get
        {
            return (object)GetValue(SelectedItemProperty);
        }
        set
        {
            SetValue(SelectedItemProperty, value);
        }
    }


    public string Label
    {
        get
        {
            return (string)GetValue(LabelProperty);
        }
        set
        {
            SetValue(LabelProperty, value);
        }

    }

    public string ImageSource
    {
        get
        {
            return (string)GetValue(ImageSourceProperty);
        }
        set
        {
            SetValue(ImageSourceProperty, value);
        }

    }

在我想使用它的 UserControl/Window 中:

<cont:AutoCompleteProperty Label="Product Category"
                            ItemsSource="{Binding Path=ProductCategories}"
                            SelectedItem="{Binding Path=ProductCategory}"
                            ImageSource="..."/>

我在以下代碼中更新了綁定....

<UserControl x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         Height="350" Width="525"
             xmlns:toolkitInput="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit"
             x:Name="root"
             >
    <Grid>![enter image description here][1]
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="3*" />
        </Grid.ColumnDefinitions>

        <Grid Grid.Column="0" Margin="0,0,2,0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>

            <Image Source="{Binding ImageSource,ElementName=root}" VerticalAlignment="Center" MaxWidth="100" Margin="1" Grid.Column="0" RenderOptions.BitmapScalingMode="HighQuality"/>

            <TextBlock Text="{Binding Label,ElementName=root}" DataContext="{Binding RelativeSource={RelativeSource Self}}" VerticalAlignment="Center"
                       HorizontalAlignment="Left" Grid.Column="1" Margin="1" TextWrapping="Wrap" Width="100" />
        </Grid>

        <toolkitInput:AutoCompleteBox FilterMode="ContainsOrdinal" IsTextCompletionEnabled="True"
                                      ItemsSource="{Binding ItemsSource,ElementName=root}"
                                      SelectedItem="{Binding SelectedItem,ElementName=root}"
                                      MinimumPrefixLength="2"
                                      MinimumPopulateDelay="300"
                                      VerticalAlignment="Center"
                                      HorizontalAlignment="Stretch" Grid.Column="1" Margin="1,1,2,1" />

    </Grid>

</UserControl>

這是使用上述代碼的 window 的圖像這是使用上述代碼的窗口圖像

我對你的綁定做了一些改動。

觀察用戶控件 DataContext。

<UserControl x:Class="WpfApplication1.AutoCompleteProperty"
             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" 
             xmlns:toolkitInput="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit"
             d:DesignHeight="300" d:DesignWidth="300" DataContext="{Binding RelativeSource={RelativeSource Self}}">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="3*" />
        </Grid.ColumnDefinitions>

        <Grid Grid.Column="0" Margin="0,0,2,0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>

            <Image Source="{Binding ImageSource}" VerticalAlignment="Center" MaxWidth="100" Margin="1" Grid.Column="0" RenderOptions.BitmapScalingMode="HighQuality"/>

            <TextBlock Text="{Binding Label}" VerticalAlignment="Center"
                       HorizontalAlignment="Left" Grid.Column="1" Margin="1" TextWrapping="Wrap" Width="100" />
        </Grid>

        <toolkitInput:AutoCompleteBox FilterMode="ContainsOrdinal" IsTextCompletionEnabled="True"
                                      ItemsSource="{Binding ItemsSource}" 
                                      SelectedItem="{Binding SelectedItem}"
                                      MinimumPrefixLength="2"
                                      MinimumPopulateDelay="300"
                                      VerticalAlignment="Center"
                                      HorizontalAlignment="Stretch" Grid.Column="1" Margin="1,1,2,1" />

    </Grid>
</UserControl>

並且文件后面的代碼沒有變化

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM