0

My code generates panels on function call defined in .cs file. There is ItemControl used in code to generates these Panels . Each Panel has it's Textbox , Slider and Combobox.

Each Panel's Slider and Combobox is playing with TextBox.Text Like:

  1. Slider to increase Textbox.Text Font Size.

  2. Combobox to Select alignment of TextBox.Text.

I want to replace Combobox with Button with Content Left. So as i click on Button it's Content must change to Right and similarly from Right to Left., to make changes to Alignment.

Can anyone solve my problem? Here Code is:

XAML FILE:

<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 File

 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));
        }
    }
}

}

4

1 に答える 1

1

TextBox.TextAlignmentプロパティは列挙型ですSystem.Windows.TextAlignment

独自の値コンバーターを実装せずにバインドする場合は、「Left」や「Right」などの同じ名前の文字列だけでなく、実際の列挙値を ComboBox に入力する必要があります...

ComboBox で TextAlignment を表現する方法は次のとおりです。

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

SelectedItemこれで、ComboBox のTextAlignmentプロパティを TextBoxのプロパティにバインドできます。

または、あなたの場合、両方を基になるデータ コンテキストにバインドする場合は、これを変更する必要があります。

private string _alignment = "Left";

これに:

private TextAlignment _alignment = TextAlignment.Left;

于 2013-10-05T19:06:57.960 に答える