带组合框的用户控件 [英] User Control with combobox

查看:62
本文介绍了带组合框的用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用TextBlock,TextBox和ComboBox创建了一个UserControl

I created a UserControl with TextBlock, TextBox and ComboBox

我有一个带有TabControl的MainWindow,并且这个tabcontrol有多个UserControl,每个TabItem一个UserControl

I have MainWindow with TabControl and this tabcontrol has several UserControls, one UserControl per TabItem

TabItem中的UserControl具有我创建的自定义UserContol.

The UserControls in TabItems have the custom UserContol that I created.

除ComboBox之外,其他所有东西都正常运行,ComboBox填充了数据项和SelectedValue,但是当我切换MainWindow上的选项卡时,与SelectedValue绑定的属性设置为null值,并且我不知道是什么在传递 null值时,我在ComboBox SelectedValue setter属性内放置一个断点,当程序到达该断点时该值显示为null,但是当我尝试继续单步执行代码时,代码退出该属性的Set部分并转到 获取部分,它执行两次,然后转到下一个对象,所以我无法弄清楚是什么传递了null值.   SelectedValue在我打开程序时会获得正确的值,但是在我切换标签页时会得到null.

Everything is working fine except the ComboBox, the ComboBox fills with data items and SelectedValue, but when I switch tabs on the MainWindow the Property that's bond to the SelectedValue gets set to null value and I can not figure out what is passing the null value, I put a break point inside the ComboBox SelectedValue setter property and the value shows null when the program hits the break point, but when I try to continue stepping through the code the code exits the Set portion of the property and goes to the Get portion, it does that twice and then goes to the next Object so I can't figure out what is passing the null value.  The SelectedValue gets correct value when I open the program, but gets nulls when I switch tabs.

我是WPF的新手,所以不确定自己在做什么.

I am new to WPF so I am not sure what I am doing.

我知道在不看到整个程序的情况下可能无法分辨出是什么原因造成的,但是也许有人遇到类似的问题,并且对如何解决这个问题提出了建议,或者至少对如何创建UserControl提出了一些建议.

I know it's probably impossible to tell what is causing this without seeing the entire program, but maybe someone had similar issue and has an advice on how to trouble shoot this or at least some suggestion on how to create an UserControl.

这是我自定义UserControl的XAML

Here's the XAML to my custom UserControl


< UserControl x:Class ="Vtsr.Views.CustomControls.VisitDataField"
              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"
              xmlns:customControls ="clr-namespace:Vtsr.Views.CustomControls"
              mc:Ignorable ="d"
              x:Name ="VisitDataFieldControl"
              d:DesignHeight ="21" d:DesignWidth ="300">

    < UserControl.Resources>
        < Style TargetType ="ComboBox" x:Key =" ComboBoxStyle">
            < Setter Property =高度"值="20"</Setter>
            < Setter Property ="FontSize"值="{Binding FontSize}"></Setter>
        </Style>

        < Style TargetType =" ComboBoxItem" x:Key =" ComboBoxItemStyle">
            < Setter Property ="BorderBrush"值=灰色"</Setter>
            < Setter Property ="BorderThickness"值="0.0"</Setter>
        </Style>

        < Style x:Key =" TextBlockStyle" TargetType ="TextBlock">
            < Setter Property ="FontSize"值="{Binding FontSize}"></Setter>
            < Setter Property ="FontWeight"值=粗体"</Setter>
            < Setter Property =保证金";值="3"</Setter>
            < Style.Triggers>
                < Trigger Property ="IsMouseOver";值="True">
                    < Setter Property ="TextDecorations";值=下划线". />
                </Trigger>
            </Style.Triggers>
        </Style>
    </UserControl.Resources>

    < Grid DataContext =" {Binding ElementName = VisitDataFieldControl}>
        < Grid.ColumnDefinitions>
            < ColumnDefinition Width =自动"></ColumnDefinition>
            < ColumnDefinition Width ="*""</ColumnDefinition>
            < ColumnDefinition Width ="100"></ColumnDefinition>
        </Grid.ColumnDefinitions>

        < TextBlock x:Uid =" TxbLabel" x:Name ="DataLabel";保证金="1,1,5,1". VerticalAlignment =中心". Width ="{Binding LabelWidth}"
                            Text =" {Binding DataLabelValue}"MinWidth ="20". Grid.Row ="0". Grid.Column ="0".样式="{StaticResource TextBlockStyle}"></TextBlock>

        < customControls:FieldTextBox Text =" {Binding DataField}" Grid.Row ="0". Grid.Column ="1".保证金="1,1,5,1". MouseRightButtonDown =" TextBox_MouseRightButtonDown"</customcustoms:FieldTextBox>
        < ComboBox ItemsSource =< {Binding UnitsList}" Grid.Row ="0". Grid.Column ="2". Style ="{StaticResource ComboBoxStyle}"
                                        ItemContainerStyle =" {StaticResource ComboBoxItemStyle}"SelectedValue ="{{Binding SelectedUnit,Mode = TwoWay}" >

        </ComboBox>
    </Grid>
</UserControl>

<UserControl x:Class="Vtsr.Views.CustomControls.VisitDataField"
             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"
             xmlns:customControls="clr-namespace:Vtsr.Views.CustomControls"
             mc:Ignorable="d"
             x:Name="VisitDataFieldControl"
             d:DesignHeight="21" d:DesignWidth="300">

    <UserControl.Resources>
        <Style TargetType="ComboBox" x:Key="ComboBoxStyle">
            <Setter Property="Height" Value="20"></Setter>
            <Setter Property="FontSize" Value="{Binding FontSize}"></Setter>
        </Style>

        <Style TargetType="ComboBoxItem" x:Key="ComboBoxItemStyle">
            <Setter Property="BorderBrush" Value="Gray"></Setter>
            <Setter Property="BorderThickness" Value="0.0"></Setter>
        </Style>

        <Style x:Key="TextBlockStyle" TargetType="TextBlock">
            <Setter Property="FontSize" Value="{Binding FontSize}"></Setter>
            <Setter Property="FontWeight" Value="Bold"></Setter>
            <Setter Property="Margin" Value="3"></Setter>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="TextDecorations" Value="Underline" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </UserControl.Resources>

    <Grid DataContext="{Binding ElementName=VisitDataFieldControl}">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"></ColumnDefinition>
            <ColumnDefinition Width="*"></ColumnDefinition>
            <ColumnDefinition Width="100"></ColumnDefinition>
        </Grid.ColumnDefinitions>

        <TextBlock x:Uid="TxbLabel" x:Name="DataLabel" Margin="1,1,5,1" VerticalAlignment="Center" Width="{Binding LabelWidth}"
                           Text="{Binding DataLabelValue}" MinWidth="20" Grid.Row="0" Grid.Column="0" Style="{StaticResource TextBlockStyle}"></TextBlock>

        <customControls:FieldTextBox Text="{Binding DataField}" Grid.Row="0" Grid.Column="1" Margin="1,1,5,1" MouseRightButtonDown="TextBox_MouseRightButtonDown"></customControls:FieldTextBox>
        <ComboBox ItemsSource="{Binding UnitsList}" Grid.Row="0" Grid.Column="2" Style="{StaticResource ComboBoxStyle}" 
                                        ItemContainerStyle="{StaticResource ComboBoxItemStyle}" SelectedValue="{Binding SelectedUnit, Mode=TwoWay}" >

        </ComboBox>
    </Grid>
</UserControl>

以下是自定义UserControl的代码

Here's code for the custom UserControl

Peter

推荐答案

与以下线程相同的问题:

Same issue as in the following thread: http://social.msdn.microsoft.com/Forums/vstudio/en-US/7ab19a63-c4cc-453d-9c48-525048beb2e2/changing-tabs-cleans-values-of-datagridcomboboxcolumn?forum=wpf


切换选项卡时,UserControl将从可视树中删除,因此您应将ComboBox的ItemsSource属性绑定到一些不属于UserControl本身的集合.例如,如果您将ObservableCollection< string> 在托管窗口类中,您可以使用RelativeSource绑定到它:


The UserControl is removed from the visual tree when you switch tabs so you should bind the ItemsSource property of the ComboBox to some collection that doesn't belong to the UserControl itself. If you for example put the ObservableCollection<string> in the hosting window class, you could bind to it using a RelativeSource:

<ComboBox ItemsSource="{Binding UnitsList, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" .... > </ComboBox>



这篇关于带组合框的用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆