Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何通过INotify属性设置wpf中文本框文本的对齐方式?_C#_.net_Wpf - Fatal编程技术网

C# 如何通过INotify属性设置wpf中文本框文本的对齐方式?

C# 如何通过INotify属性设置wpf中文本框文本的对齐方式?,c#,.net,wpf,C#,.net,Wpf,我的WPF应用程序代码在.cs文件中定义的函数调用上生成面板。代码中使用ItemControl来生成这些面板。我想通过按钮更改选定面板中定义的文本框文本的文本对齐方式。查询:我点击按钮,选择面板文本框的对齐方式从左向右和从右向左改变,现在实现对齐设置,如果选择滑块移动。代码如下: XAML文件 <ItemsControl x:Name="lstItemsClassM"> <ItemsControl.ItemTemplate> <DataTemplate&

我的WPF应用程序代码在.cs文件中定义的函数调用上生成面板。代码中使用ItemControl来生成这些面板。我想通过按钮更改选定面板中定义的文本框文本的文本对齐方式。查询:我点击按钮,选择面板文本框的对齐方式从左向右和从右向左改变,现在实现对齐设置,如果选择滑块移动。代码如下:

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

.CS文件

<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>
 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));
        }
    }
}
受保护的ObservableCollection文本=新的ObservableCollection();
int-dv;
公共窗口2()
{
初始化组件();
dv=1;
Add(new ClassM(){PKId=dv,Text=“Test 1”});
dv=2;
Add(new ClassM(){PKId=dv,Text=“Test 2”});
lstItemsClassM.ItemsSource=文本;
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
var myValue=((按钮)sender).Tag;
foreach(text.ToList()中的var f)
{
如果(f.PKId.ToString()==myValue.ToString())
{
f、 _alignment=“Right”;
MessageBox.Show(f.\u对齐);
}
}
}    
}
公共类ClassM:INotifyPropertyChanged
{
私有字符串_id;
私人密码匙;
私有字符串_文本;
私人双人房_fontSize=10;
公共字符串_alignment=“Left”;
公共密钥基础设施
{
获取{return}pkid;}
设置
{
如果(值!=\u pkid)
{
_pkid=值;
NotifyPropertyChanged();
}
}
}
公共字符串Id
{
获取{return\u id;}
设置
{
如果(值!=\u id)
{
_id=值;
NotifyPropertyChanged();
}
}
}
公共字符串文本
{
获取{return\u text;}
设置
{
如果(值!=\u文本)
{
_文本=值;
NotifyPropertyChanged();
}
}
}
公共双字号
{
获取{return\fontSize;}
设置
{
如果(值!=\u fontSize)
{
_fontSize=值;
NotifyPropertyChanged();
}
}
}
公共字符串对齐
{
获取{return\u alignment;}
设置
{
如果(值!=\u对齐)
{
_对齐=值;
NotifyPropertyChanged();
}
}
}
公共事件属性更改事件处理程序属性更改;
受保护的void NotifyPropertyChanged(字符串propertyName=”“)
{
if(PropertyChanged!=null)
{
PropertyChanged(这是新的PropertyChangedEventArgs(propertyName));
}
}
}
}


对齐意味着文本框。文本对齐
从左到右
从右到左

最好的解决方案是使用RichTextBox对齐文本。如果你想,我可以建议一个实现来读取文本字符串和格式。

那么你需要一个非字符串类型的属性对齐,并且必须绑定到TextBox.FlowDirection@LPL,这将是
System.Windows.FlowDirection
是,WPF能够在数据绑定管道中将
字符串
隐式转换为任何枚举类型,因此,您实际上不需要将VM绑定到
System.Windows
this@HighCore我希望当我点击按钮时,它的内容从左到右或从右到左改变,这些改变应该应用于文本框的流动方向。text@HighCore是的,我是说。这是否意味着只要字符串内容是LeftToRight或RightToLeft,就可以将
TextBox.FlowDirection
绑定到一个字符串,并且可以工作?是@LPL。。。我想在单击按钮时更改FlowDirection