WPF UserControl ComboBox设置SelectedValue并引发ChangeEvent

时间:2015-06-23 09:45:37

标签: c# wpf xaml combobox user-controls

我对WPF比较新,所以有一些问题。

我需要UserControls,我遇到一个包含ComboBox的问题。我需要在初始化中设置值,但它不起作用。我需要将ChangedEvent路由到MainWindow。

我的UserControl-XAML:

<UserControl x:Class="xyz.FileLineDropBox"
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" 
Name="UC">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="30" />
        <ColumnDefinition Width="110" />
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="30" />
    </Grid.ColumnDefinitions>

    <Label Grid.Column="1" VerticalAlignment="Center" Content="{Binding ElementName=UC, Path=Description}" />
    <ComboBox Grid.Column="2" VerticalAlignment="Center" HorizontalAlignment="Left" Width="40" SelectionChanged="valueChangedEventHandler" SelectedItem="Value">
        <!-- simple Alternative to Spin / NumericUpDown -->
        <ComboBoxItem Content="1" />
        <ComboBoxItem Content="2" />
        <ComboBoxItem IsSelected="True" Content="3" />
        <ComboBoxItem Content="4" />
    </ComboBox>
</Grid>

我的UserControl-Behind-Code:

namespace xyz{
public partial class FileLineDropBox : UserControl {
    ...
    //Description///////////////////////////////////////////////////////////
    public string Description{
        get { return (string)GetValue(DescriptionProperty); }
        set { SetValue(DescriptionProperty, value); }
    }

    public static readonly DependencyProperty DescriptionProperty = DependencyProperty.Register("Description", typeof(string), typeof(FileLineDropBox), new UIPropertyMetadata(""));

    //Value/////////////////////////////////////////////////////////////////
    public string Value{
        get { return (string)GetValue(ValueProperty); }
        set { SetValue(ValueProperty, value); }
    }

    public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(string), typeof(FileLineDropBox), new UIPropertyMetadata(""));

    ////////////////////////////////////////////////////////////////////////
    // Eventhandler (Text / Selection changed)
    private void valueChangedEventHandler(object sender, SelectionChangedEventArgs e){
        SelectionChangedEventArgs args = new SelectionChangedEventArgs(ValueChangedEvent, e.RemovedItems, e.AddedItems);
        RaiseEvent(args);
    }

    ////////////////////////////////////////////////////////////////////////
    // Routing the EventHandler up to the MainWindow
    public static readonly RoutedEvent ValueChangedEvent = EventManager.RegisterRoutedEvent("ValueChangedEvent", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(FileLineDropBox));
    public event SelectionChangedEventHandler ValueChanged{
        add { AddHandler(ValueChangedEvent, value); }
        remove { RemoveHandler(ValueChangedEvent, value); }
    }
}

在MainWindow-Behind-Code中我想设置Value,但它没有链接到想要的Value(Selection)。并且执行时添加EventHandler会导致错误:

    public partial class MainWindow : Window {
    FileLineDropBox     myLineDropBox;

    public MainWindow() {
        ...
        Init3_CodedComponents();
        myLineDropBox.Value                     = "4";
    }

    private void Init3_CodedComponents(){
        AddHandler(FileLineDropBox.ValueChangedEvent, new SelectionChangedEventHandler(FileLineComboBox_CntChangedEvent));  // Abo of EventHandler from FileLine
        ...
        myLineDropBox                           = new FileLineDropBox();
        myLineDropBox.Description               = "blub";
        myGrids[1].Children.Add(myLineDropBox);
        myGrids[1].RowDefinitions.Add(new RowDefinition());
        Grid.SetRow(myLineDropBox, linePos);
        ...
    }

    ...

    private void FileLineComboBox_CntChangedEvent(object sender, SelectionChangedEventArgs e){
        if(!isInitialized)return;
        ComboBox myCb = sender as ComboBox;
        int maxId;
        string sMaxId = (e.AddedItems[0] as ComboBoxItem).Content as string;
        int.TryParse(sMaxId, out maxId);
        ...
    }
}

编辑,第一个问题的解决方案如下:

  1. 在XAML中更改:

    &lt; ComboBox Grid.Column =&#34; 2&#34; ... Name =&#34; FileLineCB&#34; ... /&gt;

  2. 从背后代码中删除了值绑定代码

  3. 在Behind-Code中添加:

    public void CB_Add_Item(string NewItem) {
        this.FileLineCB.Items.Add(NewItem);
    }
    
    public void CB_Select_Item(string SelectThis) {
        this.FileLineCB.SelectedItem = SelectThis;
    }
    

1 个答案:

答案 0 :(得分:0)

问题解决了,但也许不是最好的方法。如果有一个更好或更推荐的方式,请在此处添加。

这是我的解决方案: XAML

select VehicleNumber, 
      CASE WHEN SUM(CASE WHEN DATEDIFF(d,DateTimeCommunication, DATEADD(d,-7,GETDATE())) = 0 THEN 1 ELSE 0 END) > 0 THEN 'YES' Else 'NO' END AS [7 Days Ago]
      ,CASE WHEN SUM(CASE WHEN DATEDIFF(d,DateTimeCommunication, DATEADD(d,-6,GETDATE())) = 0 THEN 1 ELSE 0 END) > 0 THEN 'YES' Else 'NO' END AS [6 Days Ago]
      ,CASE WHEN SUM(CASE WHEN DATEDIFF(d,DateTimeCommunication, DATEADD(d,-5,GETDATE())) = 0 THEN 1 ELSE 0 END) > 0 THEN 'YES' Else 'NO' END AS [5 Days Ago]
      ,CASE WHEN SUM(CASE WHEN DATEDIFF(d,DateTimeCommunication, DATEADD(d,-4,GETDATE())) = 0 THEN 1 ELSE 0 END) > 0 THEN 'YES' Else 'NO' END AS [4 Days Ago]
      ,CASE WHEN SUM(CASE WHEN DATEDIFF(d,DateTimeCommunication, DATEADD(d,-3,GETDATE())) = 0 THEN 1 ELSE 0 END) > 0 THEN 'YES' Else 'NO' END AS [3 Days Ago]
      ,CASE WHEN SUM(CASE WHEN DATEDIFF(d,DateTimeCommunication, DATEADD(d,-2,GETDATE())) = 0 THEN 1 ELSE 0 END) > 0 THEN 'YES' Else 'NO' END AS [2 Days Ago]
      ,CASE WHEN SUM(CASE WHEN DATEDIFF(d,DateTimeCommunication, DATEADD(d,-1,GETDATE())) = 0 THEN 1 ELSE 0 END) > 0 THEN 'YES' Else 'NO' END AS [1 Days Ago]
      ,CASE WHEN SUM(CASE WHEN DATEDIFF(d,DateTimeCommunication, GETDATE()) = 0 THEN 1 ELSE 0 END) > 0 THEN 'YES' Else 'NO' END AS [0 Days Ago]
from Communications
GROUP BY VehicleNumber

幕后代码:

<UserControl x:Class="xyz.FileLineDropBox"
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" 
Name="UC">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="30" />
        <ColumnDefinition Width="110" />
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="30" />
    </Grid.ColumnDefinitions>

    <Label Grid.Column="1" VerticalAlignment="Center" Content="{Binding ElementName=UC, Path=Description}" />
    <ComboBox Grid.Column="2" VerticalAlignment="Center" HorizontalAlignment="Left" Width="40" Name="FileLineCB" SelectedItem="{Binding ElementName=UC, Path=Value}" SelectionChanged="valueChangedEventHandler" />
</Grid>

}

主窗口:

namespace xyz {
public partial class FileLineDropBox : UserControl {
    ...
    //Changing ComboBox/////////////////////////////////////////////////////
    public void CB_Add_Item(string NewItem) {
        this.FileLineCB.Items.Add(NewItem);
    }

    public void CB_Select_Item(string SelectThis) {
        this.FileLineCB.SelectedItem = SelectThis;
    }

    //Value/////////////////////////////////////////////////////////////////
    public string Value{
        get { return (string)GetValue(ValueProperty); }
        set { SetValue(ValueProperty, value); }
    }

    public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(string), typeof(FileLineDropBox), new UIPropertyMetadata(""));

    ////////////////////////////////////////////////////////////////////////
    // Eventhandler (Text / Selection changed)
    private void valueChangedEventHandler(object sender, RoutedEventArgs e){
        RaiseEvent(new RoutedEventArgs(FileLineDropBox.ValueChangedEvent));
    }

    ////////////////////////////////////////////////////////////////////////
    // Routing the EventHandler up to the MainWindow
    public static readonly RoutedEvent ValueChangedEvent = EventManager.RegisterRoutedEvent("ValueChangedEvent", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(FileLineDropBox));
    public event RoutedEventHandler ValueChanged{
        add { AddHandler(ValueChangedEvent, value); }
        remove { RemoveHandler(ValueChangedEvent, value); }
    }
}