簡體   English   中英

如何通過INotify-Property在WPF中設置textbox.Text的對齊方式?

[英]How to set Alignment of a textbox.Text in wpf via INotify-Property?

我的WPF應用程序代碼在.cs文件中定義的函數調用上生成面板。 在代碼中使用了ItemControl來生成這些面板。 我想通過其按鈕更改在選定面板中定義的文本框的文本對齊方式。 查詢:我單擊按鈕,選擇面板TextBox的對齊方式從左到右,從右到左更改,如果選擇滑塊要移動,現在設置對齊方式。 這里的代碼是:

XAML文件

<ItemsControl x:Name="lstItemsClassM">
  <ItemsControl.ItemTemplate>
    <DataTemplate>
      <StackPanel Orientation="Vertical">
        <Button Content="{Binding Alignment, Mode=TwoWay}"
                Click="Button_Click"
                Tag="{Binding PKId}" />
        <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>

.CS文件

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


public class ClassM : INotifyPropertyChanged
{
    private string _id;
    private int _pkid;
    private string _text;
    private double _fontSize = 10;
    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 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));
        }
    }
}

}

對齊方式表示T​​extbox.Text left to rightright to left對齊

最好的解決方案是使用RichTextBox對齊文本。 如果您想讓我建議一個實現來讀取文字字符串和格式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM