简体   繁体   English

WPF ComboBox IsSynchronised默认值

[英]WPF ComboBox IsSynchronised Default Value

I am trying to display a default value (or even NO value) when selected index is -1, or selecteditem is null. 当选择的索引为-1或者selecteditem为null时,我试图显示默认值(甚至是NO值)。 This normally works perfectly fine but when I enable IsSynchronizedWithCurrentItem and set it to True, the first value in my DataTable gets displayed. 这通常可以正常工作,但是当我启用IsSynchronizedWithCurrentItem并将其设置为True时,我的DataTable中的第一个值会显示出来。 How can I have both IsSynchronizedWithCurrentItem ="True" and show no/default value when it is loaded. 如何同时加载IsSynchronizedWithCurrentItem =“True”并显示no / default值。

My Combo Box XAML: 我的组合框XAML:

<GroupBox Name="ClientGroup" Header="Client" Margin="63,182,0,177" FontSize="14" HorizontalAlignment="Left" Width="298">
<ComboBox Name="Supplier" Grid.IsSharedSizeScope="True" ItemsSource="{Binding}" IsEditable="True" Text="Please Choose..." TextSearch.TextPath="CompanyName" IsSynchronizedWithCurrentItem="True" Height="23" VerticalAlignment="Top" Margin="0,6,6,0" FontSize="11" StaysOpenOnEdit="True">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <Grid Margin="0,5,0,5">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" SharedSizeGroup="CompanyName" />
                    <ColumnDefinition Width="Auto" SharedSizeGroup="EIC" />
                </Grid.ColumnDefinitions>
                <TextBlock Text="{Binding CompanyName}" Grid.Column="0" />
                <TextBlock Text="{Binding EIC, StringFormat=' ({0})'}" Grid.Column="1" FontFamily="Courier New" FontWeight="Bold" FontSize="12" />
            </Grid>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>
</GroupBox>

My CS code behind: 我的CS代码背后:

ClientGroup.DataContext = (new CompanyDealsDataSetTableAdapters.CompanyTableAdapter()).GetData();

When I start my application it automatically selects the first row in my data table. 当我启动应用程序时,它会自动选择数据表中的第一行。 It works as expected when I remove IsSynchronizedWithCurrentItem. 当我删除I​​sSynchronizedWithCurrentItem时,它按预期工作。

Anyone have any solutions? 有人有任何解决方案?

Found the answer, and it was much more simple than I thought. 找到了答案,这比我想象的要简单得多。

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        Supplier.Text = null;
    }

This seems to work, and all my bound field are set to null/default values :). 这似乎工作,我的所有绑定字段都设置为null /默认值:)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM