wpf 自动完成框绑定

发布于 2024-11-18 21:28:09 字数 3783 浏览 3 评论 0原文

我创建了一些用户控件,它们包装了一些标准控件,例如:文本框/组合框+图像+文本块。我正在尝试使用 AutoCompleteBox 做同样的事情,但到目前为止失败了...... 项目列表显示正常,我可以选择某个项目,但这不会触发对 SelectedItem 的更改。我对组合框使用几乎相同的代码,所以不确定出了什么问题...

无论如何,我已经在 AutoCompleteBox 上使用了 ValueMemberPath / ValueMemberBinding,但不确定这是否是正确的方法。

UserControl 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="..."/>

I've created some UserControls which are wrapping some standard controls, for example: a textbox/combobox + Image + textblock. I'm trying to do the same thing with AutoCompleteBox and have failed so far...
The list of items is shown fine, I can select na item, but that doesn't trigger a change to the SelectedItem. I'm using almost the same code for combobox so not sure what's wrong...

Anyway I've played around with ValueMemberPath / ValueMemberBinding on the AutoCompleteBox but not sure if that's the way to go.

The UserControl 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>

The code behind:

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);
        }

    }

And in a UserControl/Window where I would like to use it:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄离 2024-11-25 21:28:09

我已经更新了以下代码中的绑定...

<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>

这是使用上述代码的窗口图像
这是使用上述代码的窗口图像

I have updated binding in the following code....

<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>

Here is a image of the window using the above code
Here is a image of the window using the above code

花开雨落又逢春i 2024-11-25 21:28:09

我对你的绑定做了一些改变。

观察用户控件 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>

并且文件后面的代码没有变化

I made few changes in your bindings.

Observe usercontrol 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>

and no changes in code behind file

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文