【问题标题】:How to Replace Combobox Binding with Button如何用按钮替换组合框绑定
【发布时间】:2013-10-05 18:43:18
【问题描述】:

我的代码在 .cs 文件中定义的函数调用上生成面板。代码中使用了 ItemControl 来生成这些 Panels 。每个 Panel 都有它的 TextboxSliderCombobox

每个 Panel 的 Slider 和 Combobox 都在玩 TextBox.Text Like:

  1. 用于增加 Textbox.Text 字体大小的滑块。

  2. 用于选择 TextBox.Text 对齐方式的组合框。

我想用内容为Left 的按钮替换组合框。所以当我点击按钮时,它的内容必须更改为Right,同样从Right 更改为Left.,以更改Alignment

谁能解决我的问题?这里的代码是:

XAML 文件:

<ItemsControl x:Name="lstItemsClassM" >
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Vertical">
                <ComboBox x:Name="cboOccupation" IsEditable="False" HorizontalAlignment="Left"
        Text="{Binding Path=Alignment, Mode=TwoWay}"
        Margin="4" Width="140">
                        <ComboBoxItem>Right</ComboBoxItem>
                        <ComboBoxItem>Left</ComboBoxItem>

                    </ComboBox>

                    <Button Content="{Binding Alignment, Mode=TwoWay}" Click="Button_Click" Tag="{Binding PKId}" SourceUpdated="Button_SourceUpdated" />
                    <TextBox x:Name="txtText" Width="300" Height="100" Text="{Binding Text;, Mode=TwoWay}" FontSize="{Binding FontSize, Mode=OneWay}" TextAlignment="{Binding Alignment, Mode=OneWay}"  />
                    <Slider Minimum="10" Maximum="30" Value="{Binding FontSize, Mode=TwoWay}" />
                </StackPanel>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

.cs 文件

 public partial class Window2 : Window
{
    protected ObservableCollection<ClassM> texts = new ObservableCollection<ClassM>();
    int dv;
    public Window2()
    {
        InitializeComponent();
        dv=1;
        texts.Add(new ClassM() { PKId=dv, Text = "Test 1" });
        dv=2;
        texts.Add(new ClassM() { PKId=dv, Text = "Test 2" });

        lstItemsClassM.ItemsSource = texts;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        var myValue = ((Button)sender).Tag;
      foreach (var f in texts.ToList())
            {
                if (f.PKId.ToString() == myValue.ToString())
                {
                    f._alignment = "Right";
                    MessageBox.Show(f._alignment);
                }
            }
    }

    private void Button_SourceUpdated(object sender, DataTransferEventArgs e)
    {
        var myValue = ((Button)sender).Tag;
       foreach (var f in texts.ToList())
        {
            if (f.PKId.ToString() == myValue.ToString())
            {
                f._alignment = "Right";
                MessageBox.Show(f._alignment);
            }
        }
    }


}


public class ClassM : INotifyPropertyChanged
{
    private string _id;
    private int _pkid;
    private string _text;
    private double _fontSize = 10;
    public bool _isChecked { get; set; }
    public string _alignment="Left";

    public int PKId
    {
        get { return _pkid; }
        set
        {
            if (value != _pkid)
            {
                _pkid = value;
                NotifyPropertyChanged();
            }
        }
    }
    public string Id
    {
        get { return _id; }
        set
        {
            if (value != _id)
            {
                _id = value;
                NotifyPropertyChanged();
            }
        }
    }

    public bool IsChecked
    {
        get { return _isChecked; }
        set
        {
            if (value != _isChecked)
            {
                _isChecked = value;
                NotifyPropertyChanged();
            }
        }
    }
    public string Text
    {
        get { return _text; }
        set
        {
            if (value != _text)
            {
                _text = value;
                NotifyPropertyChanged();
            }
        }
    }
    public double FontSize
    {
        get { return _fontSize; }
        set
        {
            if (value != _fontSize)
            {
                _fontSize = value;
                NotifyPropertyChanged();
            }
        }
    }
    public string Alignment
    {
        get { return _alignment; }
        set
        {
            if (value != _alignment)
            {
                _alignment = value;
                NotifyPropertyChanged();
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void NotifyPropertyChanged(String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

}

【问题讨论】:

    标签: c# .net wpf itemscontrol


    【解决方案1】:

    TextBox.TextAlignment 属性是枚举类型System.Windows.TextAlignment

    如果你想绑定它而不需要实现你自己的值转换器,那么你想用实际的枚举值填充 ComboBox,而不仅仅是一个具有相同名称的字符串,如“Left”和“Right”...

    以下是如何让 ComboBox 表示 TextAlignment:

    <ComboBox>
        <ComboBox.Items>
            <TextAlignment>Left</TextAlignment>
            <TextAlignment>Right</TextAlignment>
        </ComboBox.Items>
    </ComboBox>
    

    现在您可以将 ComboBox 的 SelectedItem 属性绑定到 TextBox 的 TextAlignment 属性。

    或者在你的情况下,如果你想将两者都绑定到底层数据上下文,那么你需要改变这个:

    private string _alignment = "Left";

    到这里:

    private TextAlignment _alignment = TextAlignment.Left;

    【讨论】:

    • 哦,你没有得到我的问题。我的代码运行良好我想通过按钮而不是 Combobox 来做同样的事情。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-28
    • 2011-02-01
    • 1970-01-01
    相关资源
    最近更新 更多