简体   繁体   English

WPF DatagridCombobox 列不在 combobox 样式中

[英]WPF DatagridComboboxcolumn not in combobox style

This has been a long problem of mine, and even though there are a lot of solutions out there, none has seemed to work for me.这一直是我的一个长期问题,尽管有很多解决方案,但似乎没有一个对我有用。

How do I make the DataGridComboBoxColumn display as Combobox when it is not focused?如何使DataGridComboBoxColumn在未聚焦时显示为Combobox I tried using DataGridTemplateColumn and I've almost been successful in it except that when trying to identify the selected item in the Combobox , the value returns back to previous data, and honestly made it a lot harder for me to work on this compared to just using DataGridComboBoxColumn .我尝试使用DataGridTemplateColumn并且我几乎成功了,除了在尝试识别Combobox中的选定项目时,该值返回到以前的数据,老实说,与刚刚相比,我在这方面的工作变得更加困难使用DataGridComboBoxColumn

How can I do this without using DataGridTemplateColumn ?如何在不使用DataGridTemplateColumn的情况下做到这一点? The DataGrid is found in a UserControl which is dynamically added to the MainWindow. DataGrid位于动态添加到 MainWindow 的UserControl中。

XAML XAML

<!-- This is my preferred way-->
<DataGridComboBoxColumn Header TagType x:Name="TagTypeCombo" SelectedValueBinding="{Binding TagType}"/>

<!--This is similar to all of solutions that I've seen-->
<DataGridTemplateColumn Header="Tag Type" x:Name="ComboTemplate">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <ComboBox Width="135" x:Name="Tagger" ItemsSource="{Binding tagType,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=UserControl}}" SelectedItem="{Binding Tags}" SelectedValuePath="TagType"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

Code-Behind代码隐藏

public int counter;
List<Tags> tags = new List<Tags>();
public event EventHandler SaveButtonClicked;
public List<string> tagType = new List<string>() { "Modbus Coil(0x)", "Discrete Input(1x)", "Input Register(3x)", "Holding Register(4x)" };
public ModbusCard2()
{
    InitializeComponent();
}
public class Tags
{
    public int TagAddress { get; set; }
    public string TagType { get; set; }
    public string TagID { get; set; }
}
private void DeleteButton_Click(object sender, RoutedEventArgs e) => ((WrapPanel)this.Parent).Children.Remove(this);
private void Tagger_Loaded(object sender, RoutedEventArgs e)
{
    var cx = sender as ComboBox;
    cx.ItemsSource = tagType;
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
    for (int i = 1; i <= counter; i++)
    {
        tags.Add(new Tags() { TagAddress = 0, TagID = $"M{i}", TagType = "Discrete Input(1x)" });
    }
   DevTags.ItemsSource = tags;
}

private void SaveButton_Click(object sender, RoutedEventArgs e)
{
    SaveButtonClicked?.Invoke(sender, e);
}

private void EditButton_Click(object sender, RoutedEventArgs e) => DevTags.IsReadOnly = false;

//For the SaveButtonClicked found in MainWindow
private void SaveButtonClicked(ModbusCard2 d)
{
    foreach (var a in d.DevTags.Items.OfType<Tags>().ToList())
    {
        if (a.TagType == d.tagType[1])
        {
            a.TagID = $"M{DICount}";
            DICount++;
        }
        if (a.TagType == d.tagType[2])
        {
            a.TagID = $"R{AICount}";
            AICount++;
        }
        d.DevTags.Items.Refresh();
    }
    d.DevTags.IsReadOnly = true;
}

Try this with your ComboBox that's currently inside your TemplateColumn .尝试使用当前位于TemplateColumn中的ComboBox

It adds logic in your binding to update your source trigger, while also removing your SelectedValuePath tag.它在您的绑定中添加逻辑以更新您的源触发器,同时还删除您的SelectedValuePath标记。 I'm assuming based on your lack of a DisplayMemberPath tag that your ComboBox ItemsSource is a string collection.我假设您缺少DisplayMemberPath标签,您的ComboBox ItemsSource是一个字符串集合。 If this is the case the SelectedValuePath can actually wreck your bindings (I have no idea why though).如果是这种情况, SelectedValuePath实际上会破坏您的绑定(但我不知道为什么)。

<ComboBox Width="135" x:Name="Tagger" ItemsSource="{Binding tagType,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=UserControl}}" SelectedItem="{Binding Tags, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

Let me know if you still have binding update issues after, and will go from here.让我知道您之后是否仍有绑定更新问题,并将 go 从这里开始。

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

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